have been using JS related things, but rarely summarized, today encountered the operation JS array of some problems, the JS array has a further understanding!
1. Creating an array
var array = new Array ();
var array = new Array (size);//Specify the length of the array
var array = new Array (item1,item2......itemn);//Create an array and assign a value
2, Value, assignment
var item = Array[index];//Gets the value of the specified element
Array[index] = value;//Assign a value to the specified element
3. Add new elements
Array.push (item1,item2......itemn);//Add one or more elements to the array, returning the length of the new array
Array.unshift (item1,item2......itemn);//Adds one or more elements to the beginning of the array, the original element position is automatically moved back , and the length of the new array is returned
Array.Splice (Start,delcount,Item1,item2......itemn); //Remove Delco from start position unt an element and then inserts one or more new elements from start position
4. Deleting elements
Array.pop ();//Delete the last element and return the element
Array.shift ();//Delete the first element, the position of the array element is automatically moved forward, and the deleted element is returned
Array.Splice (Start,delcount); //Remove Delco from start position Unt an element
5. Merging and intercepting arrays
Array.slice (start,end);//Returns the part of an array as an array, noting that the element of end is not included , If the end is omitted All elements after start are copied
Array.concat (array1,array2); //stitching multiple arrays into an array
6.Sorting of arrays
Array.Reverse (); //Array inversion
Array.sort (); //array sort, return array address
7. Array to String
Array.Join (separator); //Array reason is used separator connected.
The list of all the ways to delete an array element is not found! So I looked up some information and found a solution.
Deleting an array element requires extending the array prototype prototype.
array.prototype.del=function (index) {
< /c13> if (IsNaN (index) | | Index>=this.length) {
return false;
}
< /c12> for (Var i=0,n=0;i
if (This[i]!=this[index]) {
< /c55> this[n++]=this[i] ;
< /c12> }
}
this.length-=1;
};
JS Array to delete elements based on subscript