Home>Article>Web Front-end> js array operation recording method
一.splice()
Method adds/removes items to/from the array and returns the deleted item.
arrayObject.splice(index,howmany,item1,...,itemX)
Parameter | Description |
index | Required. An integer specifying the position at which to add/remove an item. Use a negative number to specify the position from the end of the array. |
howmany | Required. The number of items to delete. If set to 0, items will not be deleted. |
item1, ..., itemX | Optional. New items added to the array. |
Example:
let a=[1,2,3]; a.splice(1,1,666); console.log(a);//[1,666,3]
2.slice()
The slice() method returns selected elements from an existing array.
arrayObject.slice(start1,end2)
##Parameters | Description |
Required. Specifies where to start the selection. If negative, it specifies the position from the end of the array. That is, -1 refers to the last element, -2 refers to the second to last element, and so on. | |
Optional. Specifies where the selection ends. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to the end of the array. If this parameter is negative, it specifies the elements starting from the end of the array. |
let a=[1,2,3,4,5]; console.log(a.slice(1,4)); //[2,3,4]
3.concat()
Method is used to connect two or more arrays. This method does not change the existing array, but only returns a copy of the connected array. Return a new array. The array is generated by adding all arrayX parameters to arrayObject. If the argument to concat() is an array, then the elements in the array are added, not the array.arrayObject.concat(X,X,......,X)
Parameters | Description |
Required. This parameter can be a specific value or an array object. Can be any number. |
let a=[1,2,3]; let b=[4,5,6]; console.log(a.concat(b)); //[1,2,3,4,5,6] console.log(a.concat(4,5,6)); //[1,2,3,4,5,6]
Four.sort()
Method is used to sort the elements of the array.arrayObject.sort(sortby)
Parameter | Description |
sortby | Optional. Specifies the sort order. Must be a function.
sortbyThe function should have two parameters a and b, and its return value is as follows:
let a = [2, 4, 1, 3]; console.log(a.sort(function(a, b) { return a-b; })); //[1,2,3,4]
5. for of
1. The new function in es6 is used to traverse the array reference: Iterator and for...of loop 2. The difference between for in and for of: Simply put, for in traverses key names, and for of traverses key values.let arr = ["a","b"];for (a in arr) { console.log(a);//1,2}for (a of arr) { console.log(a);//a,b}Because of this feature of for of, it can also traverse the iterator object, while for in is a simple traversal.
5. Finally
Today I will collect the basic knowledge of js arrays. Used to consolidate one's own knowledge regularly.The above is the detailed content of js array operation recording method. For more information, please follow other related articles on the PHP Chinese website!