Native Object Array Learning
Array.from () returns an array from an array-like object or an object that can be iterated
Parameter list
Arraylike an array-like object or an object that can be iterated
MAPFN (optional) function for object traversal mapping
This (optional) Select the This object for the mapping function
var Chararr = Array.from ("abc"); Console.log (Chararr[0]); a var str = "abc"; Console.log (Str[0]); A var a = Array.from (Document.queryselectorall ("div")); Console.log (A[0]); <div>1</div> var test = new Set ("A", "B", "C"); var testget = array.from (test); Console.log (Testget[0]); A var testfun = Array.from ([1,2,3],x = x*10); Console.log (Testfun[0]); 10
The Array.isarray parameter determines whether an object is an array
var test = []; Console.log (Array.isarray (test)); True most reliable console.log (typeof test);//object console.log (Test instanceof Array);
Array.of () parameter element0 ... elementn the parameter in the array distinguishes the new array ()
var test = array.of ("str", "SSS"); Console.log (Test[0]); str var num = array.of (2); Console.log (Num[0]);//2 Console.log (num.length); The length is 1 var num2 = new Array (3); Console.log (Num2[0]); Undefined console.log (num2.length); 3
Method of Array Object Concat () combination of two or more than two arrays
When referencing objects exist in the array, be aware that
var obj = { a:1 }; var num = [1,obj,2]; Console.log (num[1]); Object{a:1} obj.a = +; Console.log (obj); OBJECT{A:100} Console.log (num[1]);//object{a:100} //reference type understand the changes pointed to
Array.entries () returns the key value pairs of the array
var num = [n/a]; var map = num.entries (); Console.log (map) //arrayiterator object console.log (Map.next (). value);//[0,1]
Array.every (Value,index,array) {},thisarg) invokes a function on each element of the array until it returns false or confirms that all members in the array satisfy the test array to return TRUE if all of them are satisfied by the end. Encountered a return false immediately stop detection returns false
The function (Value,index,array) array represents the previous array parameter so that we can modify the array object in the callback function
var obj = {min:10,max:20}; var num = [10,12,15]; var checknumrange = function(value) { if (typeof value!== ' number ') { return false; } else { return value >= this.min && value <= this. Max;}; If(Num.every (checknumrange,obj)) {Console.log (' in range '),} else {Console.log (' Out of Range ');}/ /in range about Thisarg
Array.fill (value,start,end) Start index used to populate array values the default value is 0 end for array values the default value is the length property of the This object if start is a negative start value Start+len Gth if End is negative end value is End+length
var a = [n/a]; A.fill (7); for (var I of a) { console.log (i); } 7 7 7
The Fill method overrides the previous value
The Array.filter (function (Value,index,arr) {},thisarg) filter method invokes a callback function for each of the array's values to return a new array that contains the value of the callback function for which it returns true Thisarg Optional If you do not pass the value to undefined, you can call it in the callback function through the This keyword.
var num = [1,2,5,8,10]; function Check (value) { if (value%2 = = = 0) { return true; } else { return false;}} var nums =
Array.findIndex (function (Value,index,arr), Thisarg) invokes a callback function on each value in the array until an element returns true and returns True when the element returns to its index value without returning-1
var num = [1,2,4]; var i = Num.findindex (function (value) {if (value = = 2) return true;}); Console.log (i);
Array.foreach (function (Value,index,array), Thisarg) performs the specified action for each element in the array
var num = [1,2,4]; function Test (value,index,arr) { Arr[index] = Arr[index] + 2; } Num.foreach (test); Console.log (num);
But foreach does not support break as in the above example we want Arr[index] to have a value of 2 when break or continue error (forstatement )
Array.indexof (Searchelement,fromindex) returns the index value of the first occurrence of a value in the array fromIndex an optional default of 0 not found returns 1 if the FromIndex equals or the length of the array is returned A value of -1 Fromindex is a negative start position starting at the length of the array plus fromindex and the comparison performed in IndexOf () is = = =
var num = [1,20,8,6]; var a = Num.indexof (a); Console.log (a); 1 var b = num.indexof (20,2); Console.log (b); -1 var c = num.indexof (20,-3); Console.log (c); 1
Array.join ([separator]) returns a string with all elements in the specified separator split array, separated by commas when no value is passed
var num = [2,6,10]; var test = Num.join (); Console.log (test); 2,6,10 Console.log (typeof Test)//string var test2 = Num.join ("-"); Console.log (test2);//2-6-10
Array.lastindexof (searchelement,fromindex) contrast indexof () lastIndexOf () is a descending lookup starting from the rear of an array if no FromIndex finds the entire array if f Romindex is a negative number, the length of the array + FromIndex begins the query returns the first found position no return-1
var num = [1,10,5,7,19]; var a = Num.lastindexof (7); Console.log (a); 3 var b = num.lastindexof (7,2); Console.log (b); -1
Array.map (function (Value,index,arr), Thisarg) returns an array where each element is the return value of the associated original array execution callback function
var obj = { num:2, count:function(value) { return value+this. Num; } } Defines an object The method above returns a value of one of his own variables based on the value passed in var = [Nums]; var getNum1 = Nums.map (obj.count,obj); Console.log (GETNUM1);//You can understand that each map will pass the value of Nums to the count () method of force obj//3 4 5 var ge tNum2 = Nums.map (function (value) {return this. Count (value)},obj); The value of//this represents the subsequent incoming obj console.log (getNum2);
3 4 5
Array.pop () is used to remove the last element in the array and return the element array to null to return undefined
Array.push (array) to append a new element to the end of the array returns the length of the new array
Use the pop () push () method to simulate a LIFO stack
var arr = new Array (); (Arr.push); Arr.push (3); Console.log (arr); [A] Arr.pop (); Console.log (arr); [Up]
Array.shift () removes the first element from the array and returns the element array as null returns undefined
Array.unshift (array) joins the element at the beginning of the array to return the length of the new array
Use push () shift () to simulate a FIFO stack
Array.reduce (function (previousvalue,currentvalue,currentindex,array) {},initialvalue) calls the callback function for all elements in the array The return value of the callback function is used as the cumulative result and the return value of the callback function is called in the return value for the last element that is called as the first element of the callback function.
If there is no incoming initialvalue from the second element of the array, call the callback function if the InitialValue is passed from the first element of the array to call the callback function
That is, when the initial value is not passed, the value of the Previousvalue is the value of the first element of the array currentvalue the second element of the array, the result of which is a call to the next callback function that Previousvalue passed in.
When the initial value is passed the first call, the value of Previousvalue is passed in as the initial value CurrentValue value evaluates to the value of the first element of the array as the Previousvalue incoming call to the next callback function
function Test (previous,current,index,array) { return previous + current ; } var num = [n/a]; var sum = num.reduce (test,0); Console.log (sum); 6 function Getarr (previousarr,currentvalue) { var nextarr = null; if (CurrentValue >= && Amp CurrentValue <=) {Nextarr = previousarr.concat (currentvalue);} else {Nextarr = Previousarr;} return Nextarr;} var nums = [1,26,8,12,90]; var emptyarr = []; var result = Nums.reduce (getarr,empty ARR); Console.log (result); 12 You can implement the above functions via the filter ()
Array.reduceright () vs. Array.reduce () executes this method in descending order to enable the inversion of arrays and strings
function Test (previousarr,currentvalue) { return previousarr.concat (currentvalue); } var num = [n/a]; var empty = new Array (); var result = Num.reduceright (test,empty); Console.log (result); [3,2,1]
Array.reverse () Inverse array returns the inverted array
Array.slice (Start,end) returns an array object that contains a subset of the arrays (the position copied to end does not include the end element) if start is a negative value of length+ start if End is a negative value of end+length if en D no element will be copied before start
var num = [n/a]; var B = num.slice (0,2); Console.log (b); [From] var c = num.slice (0); Console.log (c);//[1,2,3] omit end always copied to the end
Array.some (function (Value,index,array), Thisarg) confirms that the elements in the array return true for each value of the arrays every () every () method calls the callback function on the elements in the group when there is a non-conforming that returns false.
The Some () method returns True when an array element calls the callback function to return true otherwise iterates to the end of the array
Array.Sort (function () {}) sorts an array to return sorted arrays the sort () method modifies the current array without producing a new array object
function Compare (pre,next) { if (pre = = = Next) { return 0; } else if (pre >= next) { ret Urn 1; } else {return-1;}} var num = [10,1,12,100,0]; Num.sort (compare); Console.log (num);//[0 , 1,10,12,100] enables ascending and descending permutations by adjusting the returned 1-1
Array.valueof () returns a reference to the current array
var num = [n/a]; var c = num.valueof (); Console.log (c = = num); True
Array.splice (Start,deletecount,array) removes an element from an array returns the deleted element array parameter optional indicates that the inserted element is inserted from the deleted position
The return value is an array modification of the delete element that was made on the original array.
var num = [1,2,5,10]; var B = num.splice (1,2,10,20); Console.log (b); [2,5] console.log (num);//[1,10,20,10]
JavaScript Array Method Learning