Array iterative approach

Generally have foreach, every, filter, map, some, as well as newly added es6 reduce / reduceRight and find / findIndex compatible with low version of the browser is not very good

1.

was arr = [1, 2, 3 ];

arr.forEach(function (element, index, array) {
  console.log(element, index, array)
})

//output
1 0 [1, 2, 3]
2 1 [1, 2, 3]
3 2 [1, 2, 3]
View Code

forEach method which is a most basic of these methods, it is the role of a function performed for each element of the array once provided . forEach method can also pass a second argument, which is optional. If you pass a second parameter to the forEach, callback function where thiswill point to this argument. If you do not pass the second parameter is thisthe global object (the browser for the window), strict mode is undefined.

forEach method in the callback function will be followed by three arguments:

  • Array value of the current item

  • The array index for the current item

  • Array object itself

2.

every method and some methods relative, every method of all values in the array are in line with the judgment given you when conditions will return true, otherwise return false.

function isBigEnough(element, index, array) {
  return element >= 3;
}
var passed = [2, 3, 4].every(isBigEnough);
var passed2 = [3, 4, 5].every(isBigEnough);

console.log(passed); // false
console.log(passed2); // true
View Code

3.

filter to "filter", "filter" means. After the index group filter, a new array is returned after filtration. Usage and parameter almost like map.
The method is different from the map, callback function returns the filter method requires the weak equal trueor falsevalue. If true, by then, otherwise, not through.

was arr = [0, 1, 2, 3 ];

var newArr = arr.filter(function (element, index, array) {
  return e;
})

var newArr2 = arr.filter(function (element, index, array) {
  return e>=2; 
})

console.log(newArr); // [1, 2, 3]
console.log(newArr2); // [2, 3]
View Code

4.

effect map method is to map the original array into a new array in accordance with certain rules. Then its return, is to return a new array, rather than directly change the original array . Using similar methods and parameters related forEach.

callback requires a return value, and if not, like so:

was data = [1, 2, 3 ];

var arrayOfSquares = data.map(function (element) {
  element * element;
});

console.log(arrayOfSquares); // [undefined, undefined, undefined]
View Code

5.

some as long as the method is a value in the array, in line with the judgment given your condition returns true; otherwise, returns false. Usage and parameters, like the previous method.

function isBigEnough(element, index, array) {
  return element >= 4;
}
var passed = [1, 2, 3].some(isBigEnough);
var passed2 = [1, 2, 3, 4].some(isBigEnough);

console.log(passed); // false
console.log(passed2); // true
View Code

6.

reduce / reduceRight methods than the above methods to be complex; its syntax is as follows:

array.reduce (callback, [initialValue (initial value)])

Which callbackcan in turn accepts four parameters:

  • accumulatorThe last call to the callback return value, or provide an initial value ( initialValue)

  • currentValueElements in the array being processed

  • currentIndexIndexed array element being processed, if provided initialValue , starting from 0; otherwise from the beginning.

  • arrayArray object itself

It does not provide initialValue , reduce method will begin from where the index 1 callbackmethod, skip the first index. Provided  initialValue, starting at index 0.

Whether initialValuerespect to the first execution of the callback function, accumulatorand currentValuevalues in two cases: the call to provide the reduce initialValue, accumulatorthe value is initialValue, currentValuethe first array value is taken; not provided initialValue, accumulatortakes a first value in the array, currentValuetaking a second value in the array.

 

reduceRight reduce and similar, except that it is the last calculated value from the beginning.

7.

find / findIndex

The find method is used to find the first matching array members. Its parameters with the forEach method is the same; all members of the array in order to perform the callback function until you find the first member of a return value of true, then returned to the members. If there are no qualified members, returns undefined.

var value = [1, 5, 10, 15].find(function(element, index, array) {
  return element > 9;
});
var value2 = [1, 5, 10, 15].find(function(element, index, array) {
  return element > 20;
});

console.log(value); // 10
console.log(value2); // undefined
View Code

findIndex method and find similar; but it returns the index of the array element qualified. If members do not meet all the conditions, -1 is returned.

var value = [1, 5, 10, 15].findIndex(function(element, index, array) {
  return element > 9;
});
var value2 = [1, 5, 10, 15].findIndex(function(element, index, array) {
  return element > 20;
});

console.log(value); // 2
console.log(value2); // -1
View Code

 

 

Transfer from  https://segmentfault.com/a/1190000009943551

Guess you like

Origin www.cnblogs.com/caihua0405/p/11003352.html