api es5 array (understand)

First, the basic

All I'm following example, are used in the array is, so first on the front

 was arr = [1, 5, 2, 3, 6, 7, 8, 5, 4, 1, 2, 3, 2, 2];

1.arr.indexOf(v);

  • a. returns the first occurrence of the subscript
  • B. two parameters. From behind parameter represents the subscript Find
  • c. If not present, return -1.
  eg: an array of deduplication
        Analysis: establishing a temporary array, with the indexOf (), passed in the array so that each element in the temporary array each element is compared, if tempArr.indexOf (arr [i]) is -1, then to push temporary array.
        function norepeat(arr) {
            var tempArr = [];
            for (var i = 0; i < arr.length; i++) {
                if (tempArr.indexOf (arr [i]) == -1) {// is compared with each of the elements arr tempArr each element and, if not the same, into a temporary array.
                    tempArr.push(arr[i]);
                }
            }
              return tempArr;
        }
        console.log(norepeat(arr));

2.arr.lastIndexOf(v);

  • a. From looking forward after the return is the first time the number backwards subscript
  • b. Accept two parameters, the second parameter, which represents the start index from

eg:

No duplicate array detector array entry
        Analysis: Let indexOf to find from front to back, lastIndexOf from looking forward, if looking from front to back and looking forward from the back, the same index return, it indicates that there is no repeat options
        for(var i=0;i<arr.length;i++) {
            if(arr.indexOf(arr[i])== arr.lastIndexOf(arr[i])) {
                console.log(arr[i]);
            }
        }

3.arr.forEach (function (value, index, ARR) {
  // arr array entries
  // index subscript
  // arr array itself
})
NOTE: forEach () does not return value must be received by the return value of the variable

eg: an array of summing
was some = 0;
        arr.forEach(function(value,index,arr){
            // console.log(value);
            sum += value;

        })
        console.log(sum);

4.arr.map (function (value, index, ARR) {
  return value +1; + 1 // to each item in the array, and then mapped into a new array, without affecting the original array
})
// return value .

eg: Each value in the array are increased by one

where, R1 = [];
        arr1 = arr.map(function (value, index, arr) {
            return value + 1;
        })
        console.log(arr1);
        console.log(arr);

5.arr.reduce (function (ACC, CUR, index, ARR) {
  // accumulator ACC
  // value of the value of each array
  // index subscript
  // array itself ARR

  return acc + value; // accumulated
  return acc * value; // multiplicative
  return acc / value; // tired other
  return acc - value; // Accumulated
})

Note: Each quadrature sum, must have an initial value, like we sum when the sum of the initial value of 0, when Quadrature, produce an initial value of 1. In short, there are different calculations for different initial values, and reduce our approach, just to provide a totalizer, the underlying method, has set an initial value for each calculation, we only need to direct calculation.

6.arr.filter (function (value, index, ARR) {
  return filter conditions;
})

eg: a digital filter of less than 5

 res=arr.filter( function(value,index,arr) {
            return value < 5;
        })
        console.log(res);
 

NOTE: In the api m after several map (including map), each has a return value api

Guess you like

Origin www.cnblogs.com/lxz123/p/11494154.html