ES5/6 array traversal and some common methods

Source: Internet
Author: User
Tags pow prev

How to traverse an array1 ... for(vari=0;i<arr.length;i++){}------------------------------------------------------------2...arr.foreach (function(Val,index,arr) {console.log (Val,index,arr)//The third parameter is the original array can not be;}) Arr.foreach ((Val,index,arr)={Console.log ( This, Val,index,arr)//The third parameter is the original array can not be;},bind (123))//This pointer will change the arrow function;------------------------------------------------------------3...arr.map (Val,index,arr) ={console.log (Val,index,arr)//The normal need to return a value and return a new array;}) Note: It is generally necessary to have a return without the equivalent of foreach; reorganize the data structure------------------------------------------------------------4...arr.filter (Val,index,arr) ={   returnval.xxx== conditions;//filtering of unqualified elements;})-----------------------------------------------------------5...arr.some (Val,index,arr) ={   //similar to find, an element of an array matches the true}) Let arr= [A]let Newarry= Arr.some ((val,index,arr) ={   returnval==2}) Console.log (Newarry)//true;-----------------------------------------------------------6...arr.every (Val,index,arr) ={   //returns True if the array matches each element}) Let arr= [1,3,5]let Newarry= Arr.every ((val,index,arr) ={   returnVal%2==1}) Console.log (Newarry)//true;-----------------------------------------------------------7...arr.reduce (Prev,now,index,arr) ={  returnPrev+now;//the number and factorial of the array; The first argument represents the previous one, and the second represents the current}) Let arr= [A]let sum= Arr.reduce ((prev,now,index,arr) ={  returnPrev+now;//factorial math.pow (prev,now) Output}) Console.log (sum)//output 6;------------------------------------------------Let arr= [2,2,3]let sum= Arr.reduce ((prev,now,index,arr) ={  returnMath.pow (Prev,now);//factorial Prev**now}) Console.log (sum)//Output:-----------------------------------------------------------8 ... for(let Val of arr) {//Arr.keys ()//array subscript  //arr.entries ()//an item of an array}let arr1=[1,2,3,4]; for(Let a Arr1.keys ()) {Console.log (a)//Result: 0,1,2,3 iterates through the index of the array arr}--------------------------------------------------Let arr2=[1,2,3,4]; for(Let a arr2.values ()) {Console.log (a)//Result: 1,2,3,4 iterates through the values of the array arr}-----------------------------------------------------------
  Extension operators:  ... Let arr= [A]let arr2= [... arr]//ES6Let ARR3 = Array.form (arr)//ES6Let ARR4 = [].call (arr)//ES5--------------------------------------------------------------Array.form: The class array (get a set of elements, arguments) objects to the group, such as UL Li; with the length attribute; Special JSON:let JSON= { 0: "A", 1: "B", 2: "C", Length:3}let newArr2=Array.form (JSON) Console.log (NEWARR2)//["A", "B", "C"] Special string:let str= "STFSSFG"; let arr1= Str.split ("");//ES5Console.log (arr1) let arr2=array.form (str) console.log (ARR2)--------------------------------------------------------------Array.of (); Turn a set of values into a group; let Arr= Array.of ("xx1", "xx2", "xx3") Console.log (arr)//["Xx1", "xx2", "xx3"]--------------------------------------------------------------Arr.find () find out the first eligible array member; return Undefined;let arr= [1,10,20,200,90];let NEWARR= Arr.find ((val,index,arr) ={   returnval>100;}) Console.log (arr)//;--------------------------------------------------------------Arr.findindex () find location; return not found-1; let arr= [1,10,20,200,90];let NEWARR= Arr.findindex ((val,index,arr) ={   returnval>1000;}) Console.log (arr)//-1;--------------------------------------------------------------Arr.fill (filled with something, start position, end position) let arr=NewArry (10); Arr.fill ("Default value", 1,5)--------------------------------------------------------------Arr.includes () determines whether the array contains this value; let arr= [1,2,3,4];let arr1= Arr.includes (2); Cconsole.log (arr1)//true; Boolean value

ES5/6 array traversal and some common methods

Related Article

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.