What are the new methods of ECMA5 array and forEach () mock implementation

  • 2020-09-28 08:45:31
  • OfStack

The new ECMA5 array method, forEach (), is implemented in the following code example.


var o = {
       forEach: function (callback) {
         // alert(this.length);
         for (var i = , len = this.length; i < len; i++) {
           callback && callback(this[i], i, this);
         }
       },
       get length(){
         var sum=;
         for(var n in this) {
           sum+=;
         }
         return sum;
       }
     };
     Object.defineProperty(o,"length",{enumerable:false});
     Object.defineProperty(o,"forEach",{enumerable:false});
     o[] = ;
     o[] = ;
     o[] = ;
     o.forEach(function(v,i,arr){
       arr[i]=v+;
       console.log(arr[i]+"callback");
     });

It is worth noting that:

1. Use of callback functions

2. The meaning of defineProperty and defineProperties function

Both functions can define the four main properties of an object property -- value, writability, enumerability, and configurability

I'll take a moment to introduce some of the new methods for arrays in ECMA5 as follows:

While doing exercises today, I came across fitter(); Have seen these array of new methods before, but 1 has not been used in actual combat, while today in review 1;

forEaach()

This method walks through an array from beginning to end and then calls the specified function for each element in the array. This function serves as the first argument to foreach. The called function can take three arguments, the current array element, the index of the current element, and the traversal array. If there is only one argument, the argument is the current array element.


var data = [1,2,3,4,5] ;
//  Calculate the sum of the arrays 
var sum = 0 ; 
data.forEach(function(value){sum += value; }); //  Here, value  Respectively for  data[0~4];
console.log( sum ) // 15
//  Each array element adds itself 1
data.forEach(function(v, i, a){ a[i] = v + 1; }) // v  Respectively for  data[0~4]; a  Generation refers to data;
map() ;

The map() method passes each element of the called array to the specified function and returns an array (similar to the format 1 of the calling array). The tower contains the return value of the modified function. Note: it must have a return value and does not change the calling array.


var a = [1,2,3];
b = a.map(function(x) { return x * x; });
filter() 

The return value of this function is a subset of the calling function, because the function passed to it is used to make a logical judgment. If it is true, the current value is pushed into the array of subsets to be returned.


var getNum = function (a, b, k) {
return a.filter(function (v) {return b.indexOf(v) > -1;})[k-1];
}
var A = [3,4,5,6,7,8,9];
var B = [12,10,8,6];
console.log(getNum(A, B, 1))
console.log(getNum(A, B, 2));
every() some() ;

Both of these functions take a decision function that evaluates array elements and returns true or false.

In every(), the return value is true only if all array elements call the decision function and return true; A bit like & ;

In some(), true is returned whenever one of the array elements calls the decision function true.


Related articles: