vararr = [n/a];//[] is the new Array (All-in-one) syntax sugar (shorthand) varobj = {' name ': 2, ' Age ': 3};//{} is a shorthand for new Object () //var obj2 = new Object (); //obj2.name=2; //obj2.age=3; //ForEach facilitates all elements Arr.foreach (function(item,index) {//iterate over all elements of an arrayConsole.log (item)//1 2 3 })//Every determine if all elements meet all conditions varPD = Arr.every (function(item,index) {//used to determine that all array elements satisfy a condition if(item<4){ return true; }}) Console.log (PD)//true//Some determine if at least one element meets this conditionvarPD2 = Arr.some (function(item,index) {//used to determine that all array elements satisfy a condition if(item<2){ return true; }}) Console.log (PD2)//true//Sort from small to largevarArrsort =NewArray (1,4,5,7,9,3);varpx = Arrsort.sort (function(A, b) {//from small to large returnAb; //from big to small //return b-a;}) Console.log (px)//[1, 3, 4, 5, 7, 9]//Map to re-assemble elements to generate a new arrayvarCZ = Arr.map (function(item,index) {return' <b> ' +item+ ' </b> '}) Console.log (CZ)//["<b>1</b>", "<b>2</b>", "<b>3</b>"]//filter filters elements that match the criteriavarfi = Arr.filter (function(item,index) {//filter out non-qualifying array elements if(item>=2){ return true; }}) Console.log (FI)//[2, 3]
//Slice () intercepts some elements of an array var demoarr = [1,2,3,4,5,6,7,8,9,10]console.log (demoarr.slice (///// The first parameter starts at which position The following table is the second parameter starting from 0, which is from the No. 0 small label
//indexof Remove duplicate elements in the array and sort var cfarr = [' B ', ' G ', ' a ', ' C ', ' e ', ' f ', ' d ', ' a ', ' G ', ' B ']; var zzarr = []; for (var i=0;i<cfarr.length;i++) { if(Zzarr.indexof (cfarr[i]) ==-1) { Zzarr.push (Cfarr[i]) }
Common Array API ForEach every some sort map filter slice indexOf