Common methods of array

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, ...
1. Common methods of array
Iterative method of array, higher order function

1. Common methods of array

  1. 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);
  1. reverse(): reverse the order of array elements
var myarr = arr.reverse(); console.log(myarr); console.log(typeof myarr);
  1. join(): convert array to string, use "," link between default elements
var myarr = arr1.join(); console.log(myarr); console.log(typeof myarr);
  1. tostring: converting arrays to strings
var myarr = arr.toString(); console.log(myarr); console.log(typeof myarr);
  1. pop(): delete the last element of the array
arr4.pop(); console.log(arr4);
  1. push(): add one or more elements to the last face of the array
arr4.push("A","B"); console.log(arr4);
  1. unshift(): add one or more elements to the front of the array
arr4.unshift("Y"); console.log(arr4);
  1. (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. (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);
  1. 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

  1. 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);
  1. 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);
  1. 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);
  1. 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);
  1. 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);
  1. reduce(): achieve cumulative effect
let sum = arr8.reduce( (now,total) => { console.log(now,total); return now + total; }); console.log(sum);

22 June 2020, 23:53 | Views: 9852

Add new comment

For adding a comment, please log in
or create account

0 comments