Home  >  Article  >  Web Front-end  >  js array operation learning summary_javascript skills

js array operation learning summary_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:17:46975browse

shift: Delete the first item of the original array and return the value of the deleted element; if the array is empty, return undefined
var a = [1,2,3,4,5];
var b = a.shift(); 

Result 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);

Result 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 the return value of this method is unreliable. When you need to use the return value, splice can be used instead of this method. use.


pop: Delete the last item of the original array and return the value of the deleted element; if the array is empty, it will return undefined
var a = [1,2,3,4, 5];
var b = a.pop();

Result 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);

Result a: [1,2,3,4,5,6,7] b: 7


concat: returns a new array, which is formed by adding parameters to the original array
var a = [1,2,3,4,5];
var b = a.concat(6,7);

Result a: [1,2,3,4,5] b: [1,2,3,4,5,6,7]


splice(start,deleteCount,val1,val2,...): Delete deleteCount items from the start position and insert val1, val2,...
var a = [1 ,2,3,4,5];
var b = a.splice(2,2,7,8,9);

Result a: [1,2,7,8,9,5] b: [3,4]

var b = a.splice(0,1);                                                                                                                                                                                               // Same as shift > Var B = A.LENGTH-1); // The same as Pop
A.LENGTH (A.Length, 0,6,7); var b = a.Length; // The same push;

reverse:

Reverse the order of the arrayvar a = [1,2,3,4,5];var b = a.reverse();

Result a:[5,4,3,2,1] b:[5,4,3,2,1]


sort(orderfunction): Sort the array according to the specified parametersvar a = [1,2,3,4,5];var b = a.sort();

Result a: [1,2,3,4,5] b: [1,2,3,4,5]


slice(start,end): Returns a new array var consisting of items between the specified start index and the end index in the original array a = [1,2,3,4,5];var b = a.slice(2,5);

Result a: [1,2,3,4,5] b: [3,4,5]


join(separator): Combine the elements of the array into a string, with separator as the separator. If omitted, the default comma is used as the separatorvar a = [1,2,3,4,5];var b = a.join("|");

Result a: [1,2,3,4,5] b: "1|2|3|4|5"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn