Write in the front: this article mainly summarizes and summarizes some common methods of array operation. After all, the most common method in business is to get the data of the interface for operation and transmission.
ES6 Array operations
The first thing to remember about array operations is that splice, sort, and reverse are operations on the array itself that change it. Other ways to change themselves are to add/delete push/pop/unshift/shift, fill and copy fill within.
Extend operators and deconstruct assignments
Use a lot of
/ / variable exchange [a, b] = [b, a] / / obtain the residual parameters arr [...] = [a, b]... // array const obj = {... Const arr = [1, 1, 2] const arr2 = [a, b] const arr = [...arr1,...arr2] Const newArr = [...new Set(arr)] const a = [1, 2, 3, 4, 5, 5] 6] const b = [3, 4, 5, 6, 7, 8] const Values = [...new Set(a)].filter(item => b.includes(item) ) console.log(Values); / / [3, 4, 5, 6] / / data flat flat (array) const arr = [,2,2 [1], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, 14,15,66,12 []]]], 10]. [...new Set(arr.flat(Infinity))] 2.Array. From (new Set(arr.flat(Infinity))) Set(arr.flat(Infinity))].sort((a,b) => {return a - b});Copy the code
Array iteration commonly used methods and definitions
Map () : iterates through the array, lets each item in the array do one thing, and then returns the new array filter() : iterates through the array, filters the subitems that match the condition, and then returns the new array reduce() : Find /findIndex() : iterates through the array to find the first item in the array that meets the condition, and returns the subitem or subindex (to determine whether there is one). Some () : iterates through the array to see whether there is any subitem that meets the condition, and if there is one, it is true. Every () : iterates through the array to see if each subitem satisfies the condition. True is returned for all. (Judge whether it does not exist)Copy the code
The iterative method is detailed
The filter () method
/ / filtered to produce a new array of const data = [{id: 1, the title: "asura"}, {id: 2, the title: "poisson big"}, {id: 3, the title: "aragorn"}, {id: 4, the title: "la shan"}, Const hasNum = data.filter(res => {return res.id == 4; }) console.log(hasNum) //[{id:4,title:" arrobj.filter "}] let Obj = arrobj.filter (res => res.id > 2) console.log(Obj) / / / {id: 3, the title: "aragorn"}, {id: 4, the title: "la shan"}] / / filter does not comply with the conditions (in fact or in conformity with the conditions, Let Obj = arrobj.filter (res => res.id < 2) console.log(Obj) //[{id:1,title:" asuras "}]Copy the code
find/findIndex
/ / find the first eligible items or items in the array subscript const of arr = [1, 2, 3, 4, 5] const findItem = arr.find(item => item === 2) const findIndex = arr.findIndex(item => item === 2Copy the code
some
Return Boolean const arr = [1, 2, 3, 4, 5] const isinside = arr.some(item => item >= 2) // TrueCopy the code
every
/ / determine whether array all eligible, return Boolean const arr = [1, 2, 3, 4, 5] const isAll = arr.every(item => item >= 2) // false const isAll = arr.every(item => item > 0) // trueCopy the code
map
Const array = [2,3,4,5] const array2 = array.map((item) => {return item * 2; }) console. The log (array2) / /,6,8,10 [4]Copy the code
reduce
Const array = [2,3,4,5] const array2 = array.reduce((prev,next) => {return prev * next; }) console.log(array2) //120Copy the code
includes
Const Array = [1,2,3,4, 5] console.log(array.includes (1)) //true // can accept the second parameter as the start of the search console.log(testarray.includes (1,2)) //falseCopy the code
fill
// Initialize the array with the default contents. Arr.fill (value, start, End) / / fill the value of the starting position and end position const arr = arr,2,3,4,5,6,7,8,9 [1]. The fill,2,5 (7) the console. The log (arr) / / [1,2,7,7,7,6 7,8,9]Copy the code
Common methods for arrays
- Splice (start position, number to delete, content to add)
Usually used to delete data, but can also be used to replace. The return value is the deleted content
Let array = [1, 2, 3, 4]; Array. Splice (1, 2, "5"); / / (1, "5", 4)Copy the code
- push()
The last position of the array is added
- pop()
Delete the last position of the array
4.shift()
The array is deleted first
5.unshift()
The array is added first
- Sort (function)
If you don’t pass a parameter, you sort in ascending order of character encoding. If you pass a parameter, you pass a function that specifies the sort order. The function compares the two values and returns a number indicating the relative order of the two values
<! -- no parameter --> let arr = [1,3,6,4,7]; let arrSort1 = arr.sort(); console.log(arrSort1); / /,3,4,6,7 [1] the console log (arr); / /,3,4,6,7 [1] the console. The log (arrSort1 = = = arr); // this method directly changes the original array <! Let arr = [1,3,6,4,7]; let arrSort1 = arr.sort((a,b)=>{ if(a>b){ return 1; }else if(a === b){ return 0 }else{ return -1 } }); // let arr = [1,3,6,4,7]; let arrSort1 = arr.sort((a,b)=>{ if(a>b){ return -1; }else if(a === b){ return 0 }else{ return 1 } });Copy the code