Return a new array without changing the original array

1.join

Puts all the elements of an array into a string, and returns the string

const arr = ['pr','is',18]
console.log(arr.join(',')); //pr,is,18
console.log(arr); //['pr','is',18]
Copy the code

2.concat

Concatenate multiple (including two) arrays, both sides of the original array will not change, return a copy of the array concatenated, can continue concat

Const arr = [1, 2, 3, 4] const arr1 = [' pre ', 'is',' a ', 'boy'] const arr2 = [5, 6] console.log(arr.concat(arr1,arr2).concat(9)) //[1, 2, 3, 4, "pre", "is", "a", "boy", 5, 6, 9]Copy the code

3.slice

From start to end ([) close left to open right, that is, not including end) select a shallow copy of part of the array to a new array

,1,2,3,4,5,6,7,8,9 const arr = [0] the console. The log (arr. Slice (1, 5)); / / [1, 2, 3, 4] the console log (arr); / /,1,2,3,4,5,6,7,8,9 [0]Copy the code

4.map

Creates and returns a new array, each element of which is supplied by each element in the array. The original array is unchanged, and the new array is the same length as the old one

Const arr = [1,2,3,4] console.log(arr.map(I => I *10-5)); //[ 5, 15, 25, 35 ] console.log(arr.map(i => if(i<3){return i})) //[1, 2, undefined, undefined] console.log(arr.map(i => return i<3)) //true, true, false, false] console.log(arr); //[1, 2, 3, 4]Copy the code

5.every

Checks whether all elements of an array match the specified condition

  1. If one element in an array is detected to be unsatisfied, the entire expression returns false, and the remaining elements are not tested.
  2. Return true if all elements satisfy the condition;
Const arr = [1,2,3,4] console.log(arr.every(I => I >2)); //false console.log(arr.every(i => i>0)); //true console.log(arr); / / [1, 2, 3, 4]Copy the code

6.some

Checks whether an element in an array meets a specified condition

  1. If one element meets the condition, the expression returns true, and the rest of the elements are not tested.
  2. If no element satisfies the condition, false is returned;
Const arr = [1,2,3,4] console.log(arr.some(I => I >4)); //false console.log(arr.some(i => i>0)); //true console.log(arr); / / [1, 2, 3, 4]Copy the code

7.filter

Create a new array. The elements in the new array are checked by checking all the elements that match the criteria

Const arr = [1,2,3,4] console.log(arr.filter(I => I >2)); //[3,4] console.log([].filter(I => I === 'pr')); //[] console.log(arr); / / [1, 2, 3, 4]Copy the code

8.forEach

Call each element of the array and pass the element to the callback function, returning undefined

Const arr = [1,2,3,4] const copy = [] console.log(arr.foreach (I => {copy. //undefined console.log(copy); / /,4,6,8 [2] the console log (arr); / / [1, 2, 3, 4]Copy the code

9.reduce

Receiving a function as an accumulator, each value in the array (from left to right) is reduced to a value. An empty array does not execute a callback;

arr.reduce(function(pre,cur,index,arr){... },init) /** arr ** arr ** arr ** arr ** arr ** arr ** arr **/ const arr = [1,2,3,4] let sum = arr. Reduce ((pre, cur) => pre + cur, Let Max = arr. Reduce ((pre, cur) => {return Math. Max (pre, cur) => {return Math. Cur)}) //4 // Array to redo let newArr = arr.reduce((pre, cur) => {pre.indexof (cur) === -1 && pre.push(cur); return pre; }, [])Copy the code

Return a new array, change the original array

1.pop

Removes the last element of the array and returns this element (the deleted element)

  • If the array is empty, it does not change the array and returns undefined
Const arr = [1,2,3,4] const arr1 = [] console.log(arr.pop()); //4 console.log(arr1.pop()); //undefined console.log(arr); / / [1, 2, 3]. The console log (arr1); / / []Copy the code

2.push

To add one or more elements to the end of an array, return the changed array length

3.shift

Removes the first element of the array and returns it

4.unshift

Adds one or more elements to the beginning of an array. Returns the changed array length

5.reverse

Inverts the position of an element in an array element, returning a reference to that array

6.sort

Sort the elements of an array and return an array. Sorting is not necessarily stable

7.splice

Add/remove items to the array, and then return the deleted items