Home > Article > Web Front-end > How Javascript makes function usage examples of operating arrays summarized
shift : Delete the first item of the original array and return the value of the deleted element; if the array is empty, it returns undefined
var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1
unshift : Add parameters to the beginning of the original array and return the length of the array
var a = [1,2,3,4,5]; var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7
Note: The test return value under IE6.0 is always undefined, and the test return value under FF2.0 is 7, so this method The return value is unreliable. When the return value is needed, splice can be used instead of this method. This article comes from www.45it.com
pop : Delete the last item of the original array and return the value of the deleted element; if the array is empty, it returns undefined
var a = [1,2,3,4,5]; var b = a.pop(); //a:[1,2,3,4] b:5
push : Add parameters to the end of the original array, and return the length of the array
var a = [1,2,3,4,5]; var b = a.push(6,7); //a:[1,2,3,4,5,6,7] b:7
concat : Return a new array, which is composed of adding parameters to the original array
var a = [1,2,3,4,5]; var b = a.concat(6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
splice (start,deleteCount,val1,val2,...): Delete the deleteCount item from the start position and insert val1, val2,...
var a = [1,2,3,4,5]; var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4] var b = a.splice(0,1); //同shift a.splice(0,0,-2,-1); var b = a.length; //同unshift var b = a.splice(a.length-1,1); //同pop a.splice(a.length,0,6,7); var b = a.length; //同push
splice detailed explanation:
The splice function method is to remove one or more elements from an array, if necessary, in the removed Inserts a new element at the element's position and returns the removed element.
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
arrayObj is a required option. An Array object.
Start is a required option. Specifies the starting position to remove elements from the array, starting from 0.
deleteCount is a required option. The number of elements to remove.
item1, item2,. . ., itemN is required. A new element to be inserted at the location of the removed element.
The splice function method in JavaScript can modify arrayObj by removing a specified number of elements starting from the start position and inserting new elements. The return value is a new Array object consisting of the removed elements.
reverse : Reverse the array
var a = [1,2,3,4,5]; var b = a.reverse(); //a:[5,4,3,2,1] b:[5,4,3,2,1]
sort (orderfunction): Sort the array according to the specified parameters
var a = [1,2,3,4,5]; var b = a.sort(); //a:[1,2,3,4,5] b:[1,2,3,4,5]
slice (start,end): Returns a new array composed of items between the specified start index and the end index in the original array
var a = [1,2,3,4,5]; var b = a.slice(2,5); //a:[1,2,3,4,5] b:[3,4,5]
join (separator): Combine the elements of the array into a string, using separator as the separator. If omitted, the default comma is used as the separator
var a = [1,2,3,4,5]; var b = a.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
The above is the detailed content of How Javascript makes function usage examples of operating arrays summarized. For more information, please follow other related articles on the PHP Chinese website!