1. Common methods of array
- concat(): merge two arrays
var arr = [1,2,3,4,5,6,6]; var arr1 = [3, 4, 5]; var arr2 = [4, 5, 6]; var arr3 = [8, 9, 0]; var arr4 = ["w", "n", "q"]; console.log(arr); console.log(arr1); console.log(arr2); console.log(arr3); console.log(arr4); var myarr = arr1.concat(arr2); console.log(myarr);
- reverse(): reverse the order of array elements
var myarr = arr.reverse(); console.log(myarr); console.log(typeof myarr);
- join(): convert array to string, use "," link between default elements
var myarr = arr1.join(); console.log(myarr); console.log(typeof myarr);
- tostring: converting arrays to strings
var myarr = arr.toString(); console.log(myarr); console.log(typeof myarr);
- pop(): delete the last element of the array
arr4.pop(); console.log(arr4);
- push(): add one or more elements to the last face of the array
arr4.push("A","B"); console.log(arr4);
- unshift(): add one or more elements to the front of the array
arr4.unshift("Y"); console.log(arr4);
- (1) . slice(): array cutting: if arr.slice(s) There is only one parameter that represents the starting subscript value
var myarr = arr.slice(1); console.log(myarr); console.log(typeof myarr);(2) . slice(): array cutting: if arr.slice When the (- s, - e) parameter is negative, it means to split (intercept) from the last part of the array forward - 1 means the last bit - 2 table is the second last bit
var myarr = arr.slice(-2,-1); console.log(myarr); console.log(arr); console.log(typeof myarr);
- (1) . splice(): the operation of adding, deleting and modifying an array: array name. splice(i,num);
var myarr = arr.splice(1,2); console.log(myarr); console.log(typeof myarr);(2). splice(): arr.splice(i,num,"item","item"); when there are more than two parameters: operate the original array i: represent subscript num: represent number item: represent new element (new original array) will change the original array; return value is a new array: the element of the new array is composed of the element cut from the original array
var myArr = arr.splice(1,2,777,999); console.log(myArr); console.log(arr);
- indexof(): check if an element exists in the array arr.indexOf(item);
var myArr = arr.indexOf(6); console.log(myArr); var myArr = arr.lastIndexOf(6); console.log(myArr);
Iterative method of array, higher order function
- forEach(): a collection used to traverse arrays of array classes
let arr = [1,3,'Zhang San','hello']; arr.forEach((item,index) => { console.log(index + ":" + item); }); let cheks = document.querySelectorAll("input"); cheks.forEach(currentItem => { console.log(currentItem); }); console.log([...cheks]); var result = [...cheks].every( item => { return item.checked; }); console.log(result);
- map(): returns a new array of qualified elements
let arr2 = [1,2,3,4,5]; // let arr3 = arr2.map(function(item){ // return item + 1; // }); // let arr3 = arr2.map((item) => { // return item + 1; // }); let arr3 = arr2.map(item => item + 1); console.log(arr3);
- Filter(): returns an array of elements whose condition is true
let arr4 = [1,2,3,4,5]; let arr5 = arr4.filter((item) => { return item > 3; }); console.log(arr5);
- some(): returns true if one of the conditions is true
let arr6 = [11,2,51,4,45]; let result2 = arr6.some((item) => { return item > 50; }); console.log(result2);
- every(): all elements return true if the condition is true, one of them is false or false)
let arr7 = [11,2,51,4,45,0]; let result3 = arr7.every(item => item > 0); console.log(result3);
- reduce(): achieve cumulative effect
let sum = arr8.reduce( (now,total) => { console.log(now,total); return now + total; }); console.log(sum);