Recently encountered a number of problems in the project, the basic things remember not strong, summed up here to find again, to operate JS array Some of the problems of some common records!
1. Creating an array
var array = new Array ();
var array = new Array (size);//Specifies 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;//assigns 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);//Add one or more elements to the beginning of the array, the original element position is automatically moved back, return the length of the new array
Array.splice (START,DELCOUNT,ITEM1,ITEM2......ITEMN);//Remove delcount elements from start position and insert 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 delcount elements from start position backward
5. Merging and intercepting arrays
Array.slice (start,end);//Returns the part of the array as an array, noting that the end corresponding element is not included, and if the end is omitted, all elements after start are copied
Array.concat (array1,array2);//concatenation of 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);//Connect the array reason with separator
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) {
if (IsNaN (index) | | Index>=this.length) {
return false;
}
for (Var i=0,n=0;i
if (This[i]!=this[index]) {
This[n++]=this[i];
}
}
This.length-=1;
};
JS Array to delete elements based on subscript