js array of the most commonly used method (summary)

Very common array method, recording it ...

push

A back element array is appended, the length of the array returned after the addition, changing the original array

let arr = [1,2,3,4,5,6];
console,log(arr.push(7)); //7
console,log(arr); //1,2,3,4,5,6,7
复制代码

pop

Delete the element at the end of a list, returns the elements to remove, and change the original array

let arr = [1,2,3,4,5,6];
console,log(arr.pop()); //6
console,log(arr); //1,2,3,4,5
复制代码

shift

Remove the first list, returns the elements that were removed, changing the original array

let arr = [1,2,3,4,5,6];
console,log(arr.shift()); //1
console,log(arr); //2,3,4,5,6
复制代码

unshift

Add an element to the beginning of the array, the return length of the array is added, changing the original array

let arr = [1,2,3,4,5,6];
console,log(arr.shift(0)); //7
console,log(arr); //0,1,2,3,4,5,6
复制代码

splice (start position, the number of deleted elements)

//删除
let arr = [1,2,3,4,5,6];
arr.splice(0,2);  //[1,2]
console.log(arr); //[3,4,5,6]
//替换
let arr = [1,2,3,4,5,6];
arr.splice(0,0,'a','b');
console.log(arr); // ["a", "b", 1, 2, 3, 4, 5, 6]
复制代码

join()

Array into a string, this parameter is a parameter as a separator, the default parameters are comma-delimited does not pass does not change the original array

let arr = [1,2,3,4,5,6];
arr.join(); //"1,2,3,4,5,6"
console.log(arr); // [ 1, 2, 3, 4, 5, 6]
复制代码

Here referring to the next turn an array of strings split

let str = '123456';
str.split(''); //[1, 2, 3, 4, 5, 6]
复制代码

sort()

To sort elements in the array, the array change the original, default sorted by the first character, instead of numbers sorted by size, for example:

let arr = [1,22,88,9,10];
arr.sort(); //[1, 10, 22, 88, 9]
复制代码

Well, not as we imagine, then how to change it the next? This is when you need to sort () function parameter passing, a transfer function.

let arr = [1,22,88,9,10];
arr.sort(function(a,b){
    return a-b;
});  //[1, 9, 10, 22, 88]  a-b 代表从小到大的顺序排列

arr.sort(function(a,b){
    return b-a;
});  //[88, 22, 10, 9, 1]   a-b 代表从大到小的顺序排列
复制代码

reverse()

Inverted order of the array

let arr = [1,22,88,9,10];
arr.reverse();  //[10, 9, 88, 22, 1]
复制代码

concat()

Connecting the two arrays, an array of return after the connection, without changing the original array

let arr = [1,2,3];
let arr2 = [4,5,6];
arr.concat(arr2);   //[1,2,3,4,5,6]
复制代码

indexOf()和 lastIndexOf()

Finding an element index value, if repeated, is found in the first index value if there returned, return -1; lastIndexOf () the same, only the beginning to find the end of the array.

let arr = [1,2,3,4,5,2]
let arr1 = arr.indexOf(2)
console.log(arr1)  // 1
let arr2 = arr.indexOf(9)
console.log(arr2)  // -1

let arr = [1,2,3,4,5,2]
let arr1 = arr.lastIndexOf(2)
console.log(arr1)  // 5
let arr2 = arr.lastIndexOf(9)
console.log(arr2)  // -1
复制代码

Array.from()

Converting the pseudo-array (array data) into an array, there is provided a length attribute

let oLi = document.querySelectorAll('li');
Array.from(oLi);
console.log(oLi); //[元素1,元素2,元素3]
复制代码

Array.of()

The method of converting a set of values ​​is an array, regardless of the type of the parameter, the number of points only, in an amount of 0 returns an empty array

let arr1 = Array.of(1,2,3);	
let arr2 = Array.of([1,2,3]);
let arr3 = Array.of(undefined);
let arr4 = Array.of();
console.log(arr1); // [1, 2, 3]
console.log(arr2); // [[1, 2, 3]]
console.log(arr3); // [undefined]
console.log(arr4); // []
复制代码

Array.forEach(function(item,index,arr){},thisArr)

  • item: Required, the current element
  • index: Optional, the current index
  • arr: Optional, the current array object element belongs.
  • thisArr: an array of pointers,

Through the array, the return value is no return, is sometimes used instead for.

let arr = [1,2,3,4,5,6];
arr.forEach(function(item,index){
    console.log(item);
});
复制代码

Array.map()

Value refers to the array of mappings (iterate), has a return value, the method returns a new array, the array element calls a function of processing the original array elements

var arr = [1,2,3,4];
var arr2 = arr.map(function(item){
    return item*item;
});
console.log(arr2);  // [1, 4, 9, 16]
复制代码

Array.filter()

Filter array and returns a new array, return condition is an element of true, false elements to filter out

let arr = [1,2,3,4,5]
let arr1 = arr.filter(function(item,index){
    return item > 1;
})
console.log(arr1); //[2, 3, 4, 5]
复制代码

Array.reduce()和Array.reduceRight()

arr.reduce (function (total, cur, index, arr) {}, initialValue) Both methods all iterations array, and then generate a final return value. Accumulator, reduce from left to right, reduceRight from right to left.

  • total initial value, also the accumulated value of a callback function returns, or starting values.
  • cur current array entry in the array being processed.
  • The current index value index array item in the array.
  • array original array.
  • By default the first performance, total represents the first argument, cur represents the second parameter. Be provided if the reduce initialValue call (), then the first preValue initialValue equal, and equal to the first value curValue array; initialValue if not provided, then preValue equal to the first value in the array.
let arr = [0,1,2,3,4]
let arr1 = arr.reduce((total, cur) => 
    return total + cur;
)
console.log(arr1)    // 10
复制代码

Array.every()

Each item in the array to determine whether the condition is satisfied only if all items are to meet the conditions, will return true.

let arr = [1,2,3,4,5]
let arr1 = arr.every( (i, v) => i < 3)
console.log(arr1)    // false
let arr2 = arr.every( (i, v) => i < 10)
console.log(arr2)    // true
复制代码

Array.some()

And every method should be regarded as a method brothers. As long as there is a matched, returns true.

let arr = [1,2,3,4,5]
let arr1 = arr.some( (i, v) => i < 3)
console.log(arr1)    // true
复制代码

find()

The method of each element in the array calls a function performed by the return value of the first element of the array test (the function determination) FIG.

let arr = [1,2,3,4,5,2,4]
let arr1 = arr.find((value, index, array) =>value > 2)
console.log(arr1)   // 3
复制代码

findIndex()

let arr = [1,2,3,4,5]
let arr1 = arr.findIndex((value, index, array) => value = 1)
console.log(arr1)  // 0
复制代码

Array.includes(seachVal,index)

The method used to determine whether an array contains a specified value, if so returns true, otherwise false.

  • To find the value of search
  • Find seachVal index indicates the start from the next table, if negative, in ascending order starting from the array.length + fromIndex the index search. The default is 0.
let arr = [1,2,3,4,5]
let arr1 = arr.includes(2)  
console.log(arr1)   // ture
let arr2 = arr.includes(6) 
console.log(arr2)    // false
let arr3 = [1,2,3,NaN].includes(NaN)
console.log(arr3)  // true
let arr4 = ["a","b","c","d"];
let result = arr4.includes("b",-1);
console.log(result);  // false
let arr5 = ["a","b","c","d"];
let result1 = arr5.includes("b",-3);
console.log(result1);  // true
复制代码

Reproduced in: https: //juejin.im/post/5cf631756fb9a07ec42b47fa

Guess you like

Origin blog.csdn.net/weixin_33725515/article/details/91455731