Defined
var empty=[];
When to use
When the property name is a small, contiguous integer, the data group is used, otherwise the object is used.
Recognition
The typeof operator has no meaning to the data because it returns ' object '
ECMA5 The method has been added
You can add static methods:
array.isarray=function(obj) { return Object.prototype.toString.apply (obj) = = = ' [ Object Array] '}
Length
The value of the length property is the maximum integer property name of this array plus 1:
Array=[];
array[7]=0;
Console.log (Array.Length);
Setting a larger length does not allocate more space to the array (undefined), while the length setting teenager causes all the subscripts equal to the new length property to be deleted.
Add to
Add a single element:
Empty[empty.length]= "";
Empty.push ("");
Delete
Delete Numbers[2]; Leave a void
Numbers.splice (2,1); Move forward
Find
ie9+ Support
Array.indexof ()
Array.lastindexof ()
Output
Alert ( array.tostring ()), Alert (array.valueof ()), alert (array), alert (Array.join (', '));
Method
Array.concat ()//connection string, equivalent to ' + '
Array.join ()//Generate string
Array.push ()
Array.pop ()
Array.shift ()
Array.unshift ()
Array.reverse ()//reverse Sort
Array.Sort ()//String sort
Array.slice ()
The Array.splice ()//is quite powerful and can be used to make arbitrary increases and deletions.
Sort
There is a sort method (Array.prototype.sort) in the data prototype, but only the characters are sorted
functionSortnumber (A, b) {returnAB}vararr =NewArray (6) arr[0] = "10"arr[1] = "5"arr[2] = "40"arr[3] = "25"arr[4] = "1000"arr[5] = "1"document.write (arr+ "<br/>")//character Sortingdocument.write (Arr.sort () + "<br/>")//1,10,1000,25,40,5//Sorting Datadocument.write (Arr.sort (Sortnumber))//1,5,10,25,40,1000
Iteration
Only simple usage is described here.//The following only supports ECMA5
// each satisfies a condition that returns true // returns a sub-array that satisfies a condition // operates on each element and does not return a // operates on each element, returns the result array some ()// has a satisfied condition that returns True
Merge
The current latter has a relationship with the implementation of the operation, to facilitate the realization of the function of the two-way list. The following only supports ECMA5
Reduce () reduceright ()
Coding specifications
The following from GitHub, there are a lot of good programming styles about JavaScript, recommend you see: Https://github.com/airbnb/javascript
Use the literal syntax for array creation.
Badvar items =newarray ();//Goodvar items = [];
Use Array#push instead of direct assignment to add items to an Array.
var somestack = [];//badsomestack[somestack.length] = ' abracadabra ';//Goodsomestack.push (' Abracadabra ');
When your need to copy a array use Array#slice. JsPerf
var len = Items.length;var itemscopy = [];var i;//badfor (i =0; i < Len; i++) { itemscopy[i] = items[i];} Gooditemscopy = Items.slice ();
To convert a Array-like object to a array, use Array#slice.
Functiontrigger () { var args =array.prototype.slice.call (arguments); ...}
A concise summary of JavaScript arrays