One: Array arrays
1, Array.isarray (parameter) detection is an array,*incompatible with IE8, compatible with IE9 and above, Chrome, Firefox, etc., to be compatible with IE8, you can use
Object.prototype.toString.call ([up]) = = "[Object Array]"
2. Example method:
1). Push (Xx,xx,xx,...) Insert one or more items to the end of the array and modify the length
with arr[arr.length] = xxx; Insert a similar
var arr = [];arr.push (3,4); Console.log (arr); // [1,2,3,4]arr[arr.length] = 5; Console.log (Arr[arr); // [1,2,3,4,5]
2). Pop ()//Remove the last item from the end of the array and modify the length
3). Shift ()//Remove the first item from the head of the array
4). Unshift (Xx,xx,xx,...) Insert one or more items from the head of an array
5). Reverse ()//inverted array
var arr = [1,2,3,4,5];arr.reverse (); Console.log (arr); // [5,4,3,2,1]
6). Sort ()//sorting
1, sort, is the array of each item ToString () into a string, and then compare the encoding, so:
var arr = [0, 1, 5, ten, +]; Arr.sort (); Console.log (arr ) ; // [0, 1, ten, 5]
2, if you want to sort by number size, if you do?
var arr = [0, 1, 5, ten, +]; function Compare (value1, value2) { if(value1 < value2) { return -1; } Else if (value1 > value2) { return 1; } Else { return 0; }} Arr.sort (Compare); Console.log (arr); // [0, 1, 5, ten, the]
In fact, it can be simply written as:
var arr = [0, 1, 5, ten, +]; // Value1-value2 is ascending // value2-value1 is descending function Compare (value1, value2) { return value1- value2;} Arr.sort (Compare); Console.log (arr); // [0, 1, 5, ten, the]
3. Randomly scrambled arrays:
var arr = [0, 1, 5, ten, +]; // math.random () The value returned each time is different function Compare (value1, value2) { return math.random ()-0.5;} Arr.sort (Compare); Console.log (arr);
4, according to the object of an item, to sort
vararr = [{ "A": 5, "B": "A1"}, { "A": 10, "B": "A2"}, { "A": 1, "B": "A3"}];arr=Compare ({Arr:arr, prop:A, type:' Down '}); Console.log (arr);functionCompare (options) {if(typeofOptions.arr = = "Array" && options.arr.length = = 0){ return []; } Options.type= Options.type | | "Up"; var_prop =Options.prop; Options.arr.sort (function(value1, value2) {if(Options.type = = "Up"){ returnValue1[_prop] >Value2[_prop]; }Else if(Options.type = = "Down"){ returnValue2[_prop] >Value1[_prop]; } }); returnOptions.arr;}
7). Concat (parameter) [return new array]
var arr = [+]; var // Replication Features Console.log (ARR2); var // Merging array functions Console.log (ARR3); var // Insert item and merge Array function Console.log (ARR4);
8). Slice (start position, end position * not included)[return new array]
Intercept a new array, parameter description: Start and end positions, calculated from 0; End position if not, the last array item is truncated
9). The most powerful method in the splice () array
//1: Delete any number of items, splice (starting from the first few items, deleting several items)vararr = [1,2,3,4,5];arr.splice (1, 2); Console.log (arr); //[1, 4, 5]//2: Inserts any number of items to the specified location, splice (starting with the first item, 0, inserting item 1, inserting Item 2, ...)vararr = [1,2,3,4,5];arr.splice (2, 0, ' A ', ' B ') Console.log (arr); //[1, 2, ' A ', ' B ', 3, 4, 5]//3: Inserts any number of items to the specified location, splice (starting with the first few items, deleting several items, inserting item 1, inserting Item 2, ...)vararr = [1,2,3,4,5];arr.splice (2, 1, ' A ', ' B ') Console.log (arr); //[1, 2, ' A ', ' B ', 4, 5]
Note: the splice () method always returns a new array containing the items that were deleted from the original array.
8). indexOf (Find the entry, look for the starting position) from the front to the back, LastIndexOf (the item to find, find the starting position) from the back to look forward;* Incompatible IE8
var arr = [1,2,3,1,2,33,1,44]; var idx = arr.indexof (3var aidx = []; var idx = 0; while (idx = arr.indexof (1, idx))! =-1) { aidx.push (idx); + =1// [0, 3, 6]
9) Iteration method in ES5, total 5: *IE8 and below are incompatible
Every (): loops each item of the given array, returns True if all items satisfy the inside of the function, or false otherwise. does not affect the original array.
Most (): loops each item of the given array, returns true if at least one item satisfies the inside of the function, otherwise returns FALSE. does not affect the original array.
Filter (): Loops each item of the given array, returns all the array items that satisfy the condition within the function, and makes up a new array to return. does not affect the original array.
Map (): Loops each item of the given array, runs the given function, returns the result of each function call to make a new array and returns. does not affect the original array.
ForEach (): loops through each item of the given array, running the given function to handle the original array item. The method does not return a value and affects the original array.
varNums = [1,2,344,55,6,6,32];varevery = Nums.every (function(item, index, array) {returnItem > 2;}); Console.log (' Every: ', every);//Every:falsevarsome = Nums.some (function(item, index, array) {returnItem > 300;}); Console.log (' Some: ', some);//some:truevarFilter = Nums.filter (function(item, index, array) {returnItem%2==0;}); Console.log (' Filter: ', filter);//filter: [2, 344, 6, 6, +]varMap = Nums.map (function(item, index, array) {returnItem*2;}); Console.log (' Map: ', map);//map: [2, 4, 688, +,--]varForEach = Nums.foreach (function(item, index, array) {if(Item < 10) {Array[index]= 10; }}); Console.log (' ForEach: ', nums);//ForEach: [Ten, ten, 344, +, ten, ten]
Elevation three: Array