Array Method Two

 filter: filter, do not operate the original array, returns an array of filtered, returns true, this one would put the new array

General for deleting

let newAry=[3,5,2,8,9,4,0].filter(function (item){

   return item>2 && item<5

})
console.log(newAry);//[3、4]

forEach: cycles, no return a return value

ARR = the let [2,4,5,7,1 ]; 
arr.forEach ( function (Item) { // declarative, not concerned with how to achieve 

   the console.log (ITME); 
}) 
for (the let I = 0; I <arr.length; i ++) {// programmatically
the console.log (ARR [I]);
}

for (Key in the let ARR) {// Key string type will become

}
for (ARR of the let Val) {} // support return, and the value of the array, can not traverse the object
face questions: forEach, for, for in, for of difference
for in an array of private property can be printed
for of support for return, can not traverse the object

includes (ES6): Check whether the array contains return type boolean

let arrf = [2,5,7,9];
arrf.includes(5);

find (ES6): find specific that one, does not change the original list, returns true if found, to find the end of the cycle, can not find the returns undefined

@ Demand: Username found 
the let resule = arr3.find ( function (ITME, index) { return ITME == 'XXX' })
the console.log (resule); // XXX

keys: Each method returns an array index key contained Array Iteratorobjects.

var array1 = ['a', 'b', 'c'];
var iterator = array1.keys(); 
  
for (let key of iterator) {
  console.log(key); // expected output: 0 1 2
}

map: mapping, mapping the original array into a new array, the original array is not operated, returns a new array, what the callback function returns this one is what

Generally used to update, modify,

// demand [1,2,3] into <Li>. 1 </ Li> <Li> 2 </ Li> <Li>. 3 </ Li> 
the let of arr1 = [1,2,3] .map (function (Item) {
return `<Li> ITME $ {} <Li> //` es6 the template string encountered variables $ {value}
})
the console.log (arr1.join ( ''));

reduce: convergence, four parameters, return the result of superposition of the original array unchanged, the callback function returns

let some = [1,3,4,5] .reduce ( function (prev, next, index, item) {// first, second, indexing, the original array
    the console.log (PREV, Next);
    return + Next PREV
})
the console.log (some);

 

// calculate multidimensional arrays

= some2 the let [{Pirce: 30, COUNT: 2}, {Pirce: 50, COUNT: 2}, {Pirce: 40, COUNT: 2}];
the let some3 = some2.reduce (function (PrVE, Next) {
    return * + next.pirce next.count PrVE;
}, 0) // 0 is the default for the first time specified PrVE
the console.log (some3); // 240

 

// multidimensional array becomes a one-dimensional array

let some4 = [[1,2,3],[4,5,6],[7,8,9]];

let some5 = some4.reduce(function (prve,next){

   return prve.concat(next);

})

console.log(some5);//[1,2,3,4,5,6,7,8,9]

some: find after returning turn, find true stop, can not find the returns false

every: After finding false stop, find returns false

 

Guess you like

Origin www.cnblogs.com/xinxinxiangrong7/p/11426580.html