ECMA5 array addition methods and forEach () imitation implementation _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to add ECMA5 arrays and the implementation of forEach () imitation, you can refer to the following code example to introduce the new method forEach () for ECMA5 arrays. the specific code is as follows,

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. Significance of defineProperty and defineProperties functions

Both functions can define four features of object attributes-value, writability, enumerative, and configurability.

The following describes how to add arrays in ECMA5 as follows:

Today, when I was doing exercises, I met fitter (). I used to read the new methods of these arrays, but I have never used them in practice. I will review them today;

ForEaach ()

This method traverses an array from start to end and calls the specified function for each element in the array. This function is the first parameter of foreach. The called function can have three parameters: the current array element, the index of the current element, and the array to be traversed. if there is only one parameter, this parameter is the current array element.

Var data = [1, 2, 3, 4, 5]; // calculate the sum of the array and var sum = 0; data. forEach (function (value) {sum + = value ;}); // The value here represents data [0 ~ 4]; console. log (sum) // 15 // add 1data to each array element. forEach (function (v, I, a) {a [I] = v + 1 ;}) // v represents data [0 ~ 4]; a represents data; map ();

Map () methodPass each element of the called array to the specified function and return an array (the same format as the called array). The tower contains the return value of the function. note: it must have a return value and will not change the array called.

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 called function, because the function formula passed to it is used for logical judgment. if it is true, the current value is pushed into the subset array 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() ;

The parameters of these two functions are both a judgment function that judges the array elements and returns true or false;

In every ()The returned value is true only when all array elements call the decision function and return true &;

In some ()If an array element is called to determine whether the function is true, true is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.