The Array object is a composite type used to store multiple values in a single variable, each of which can be of different value types.
Ways to create an array object:
New Array (); New Array (size); New Array (element0, Element1, ..., ELEMENTN);
1. when the index value is negative, it is treated as a property of the object, and if it is a non-negative numeric string, it is implicitly converted to a numeric index:
var New Array (); a[-1.23]=true; a[1]= "pomelo"; a["]=" "tt"; Console.log (a); // / [1: "Pomelo", +: "tt", -1.23:true] Console.log (a.length) ///101 The length of the sparse array is the maximum subscript +1
In fact, the index of the array object is actually a special form of the object property name, when trying to query any object property name, will not error, but will display undefined:
Console.log (a["Hello"]); // undefined
2. The Length property can be used to place an empty array or to intercept an array from the front:
var array1=[1,2,3,4,5]; Array1.length=3; // /[1,2,3] array1.length=0; // /[] array1.length=5; // /[] Empty array of length 5 Console.log (array1)
You can use the Object.defineproperty (object, property name, {settings}) method to set the readable writable:
Object.defineproperty (array1, "Length", {writable:false}); Array1.length=10; Console.log (array1.length); // /5
3. Adding and removing array elements:
End: add element push (), delete element pop ();
Header: add element unshift (), delete element shift ();
var array2=[1,2,3,4,5]; Array2.unshift (6); // /[6,1,2,3,4,5] Array2.shift (); // [1,2,3,4,5] Console.log (array2);
4. Convenient optimization and selection of arrays
When the size of the array is large, length should not be queried in each loop;
When you need to filter invalid data in the array, use continue to jump out of the loop;
var array3=[null,1,0,undefined];
for (var i= 0,len=array3.length;i<len;i++) {
if (!array3[i]) continue;
Console.log (Array3[i]); 1
}
or use the foreach () method that comes with ES5:
var array4=[1,2,3,4]; var sum=0; Array4.foreach (function(i) { sum+ =i; }); Console.log (sum); // /10
5.js does not support multidimensional arrays, but can be implemented by arrays of arrays:
var array5=New Array (ten); for (var i=0;i<array5.length;i++) { array5[i]=new Array (ten); } for (var i=0;i<array5.length;i++) { for (var j=0;j<array5[i]. length;j++) { array5[i][j]=i*j; } } Console.log (array5[5][6]); // -
6. Array methods
6.1 Join () converts an array into a string and joins together
var array6=[1,2,3]; Console.log (Array6.join ("-")); // 1*2*3 Console.log (Array6.join ("")); // 123 Console.log (Array6.join ()); // the Console.log (typeof Array6.join ()); // String
6.2 Reverse () reverses the order of elements based on the original array itself
var array7=[1,2,3];
Console.log (Array7.reverse ()) //[3,2,1]
6.3 sort () array sorting
var array8=["Apple", "Cherry", "cherry"]; Console.log (Array8.sort ()); // ["Apple", "Cherry", "cherry"] array8.push (undefined); Console.log (Array8.sort ()); // undefined will be put to the end ["Apple", "Cherry", "cherry", undefined]
If you want to change the collation, you need to pass in a comparison function:
var array9=[33,4,11,222]; Console.log (Array9.sort ()); // 11,222,33,4 Default compares Console.log with the first number (Array9.sort (A, b) { // arrange return from small to large ( b; }) )
Character sorting is small in size by default, with the first letter ASCII encoded as the basis, from small arrivals to sort:
vararray10=["Ant", "Boy", "Cat", "Dog"]; Console.log (Array10.sort ()); //["Boy", "Dog", "Ant", "cat")Console.log (Array10.sort (function(s,t) {varA=s.tolowercase (); varb=t.tolowercase (); if(A<B)return-1; if(A>B)return1; return0; }) ); //["Ant", "Boy", "Cat", "Dog")
6.4 concat () array connection
var array11=[1,2,3]; Console.log (Array11.concat (4,5)); // [1,2,3,4,5] Console.log (Array11.concat ([4,5])); // [1,2,3,4,5] Console.log (Array11.concat ("4", [5,[6,[7,8]])) //[1, 2, 3, "4", 5, array[2]]
6.5 Slice (first position, second position)
Returns a sub-array from the first position (including) to the second position;
Returns a subarray of all elements from the specified position to the end of the array, if only one argument is drawn;
If the parameter has a negative number, it identifies the subscript from the position of the back-forward;
var array12=[1,2,3,4,5]; Console.log (Array12.slice (0,3)); // [A] Console.log (Array12.slice (3)); // [4,5] Console.log (Array12.slice ( -1)); // [5] Console.log (Array12.slice (1,-1)); // [2,3,4]
6.6 Splice (The starting position of the insertion or deletion, the number of elements removed) a common way to insert, delete, or replace elements in an array:
vararray13=[1,2,3,4,5,6,7,8]; //DeleteConsole.log (Array13.splice (4));//[5,6,7,8]Console.log (ARRAY13);//[1,2,3,4]Console.log (Array13.splice);//[2,3]Console.log (ARRAY13);//[1,4] //InsertArray13.splice (1,0, "pomelo"); Console.log (ARRAY13); //[1, "Pomelo", 4] //Replace vararray14=[1,2,3,4,5]; Array14.splice (2,1, "pomelo");//[1, 2, "Pomelo", 4, 5]Console.log (ARRAY14)
6.7 map () passes each element of the array that calls this function to the specified function:
var array15=[1,2,3]; var array16=array15.map (function(i) { return i*i; }); Console.log (ARRAY16) //[1,4,9]
-------------Ongoing Updates-----------
JavaScript array detail (JS array depth parsing)