An overview of the properties and methods contained in the js array container

1. .length----Get the length of the array;

var arr = [1,2,3,4,5];
console.log(arr.length)  //5

2. shift()---- delete the first item of the original array, and return the value of the deleted element; if the array is empty, return undefined, and modify it directly on the element array;

var arr= [1,2,3,4,5];
var result = arr.shift(); 
console.log(arr)   //[2,3,4,5]
console.log(result)   //1

3. unshift()----Add the parameter to the beginning of the original array, and return the length of the array, modify it directly on the array;

arr = [1,2,3,4,5];
result = arr.unshift(-2,-1);

console.log(arr) //[-2,-1,1,2,3,4,5]
console.log(result) //7
4. pop()---- delete the last item of the original array, And return the value of the deleted element; if the array is empty, return undefined, and modify it directly on the meta array;

arr= [1,2,3,4,5];
result = arr.pop();
console.log(arr) //[1,2,3,4]
console.log(result) //5

5. push()----Add the parameter to the end of the original array, and return the length of the array, modify it directly on the meta array;

arr = [1,2,3,4,5];
result = arr.push(6,7);
console.log(arr) //[1,2,3,4,5,6,7]
console.log(result) //7

6. concat()----array merge, return a new array without affecting the original array;

arr = [1,2,3,4,5];
result = arr.concat(6,7);
console.log(arr); //[1,2,3,4,5]
console.log(result ); //[1,2,3,4,5,6,7]
7. sort()---- sort the array from small to large, and modify it directly on the original array;

arr = [4,5,2,3,1];
result = arr.sort();
    console.log(arr);   //[1, 2, 3, 4, 5]
console.log(result)   //[1, 2, 3, 4, 5]

8. reverse()---- reverse the order of the array and modify it directly on the original array;

arr = [4,5,2,3,1];
result = arr.reverse()
console.log(arr);   //[1, 3, 2, 5, 4]
console.log(result)   //[1, 3, 2, 5, 4]

9. slice(startIndex,endIndex)----Intercept the items between the start index startIndex to the end index endIndex to form a new array, the new array includes the items corresponding to the start index startIndex, but does not include the end index corresponding to endIndex item;

arr = [1,2,3,4,5];
result1 = arr.slice(2,5);
result2 = arr.slice(2);
console.log(arr)  //[1,2,3,4,5]
console.log(result1)  //[3,4,5]
console.log(result2)  //[3,4,5]

10. join(separator)---- group the elements of the array into a string, with the separator as the separator, if omitted, the default comma is used as the separator, which does not affect the original array;

arr= [1,2,3,4,5];
result = arr.join("|");
console.log(arr)  //[1, 2, 3, 4, 5]
console.log(result)  //1|2|3|4|5

11. splice(start,deleteCount,val1,val2,...)----delete the deleteCount item from the start position, and insert val1,val2,... from this position, and modify it directly on the original array;

arr= [1,2,3,4,5];
result1 = arr.splice(2,2,7,8,9);
console.log(arr);   //[1,2,7,8,9,5]
console.log(result1);  //[3,4]
result2 = arr.splice(0,1);  //同shift
console.log(arr);   //[2,7,8,9,5]
console.log(result2);  //[1]
arr.splice(0,0,-2,-1);   //同unshift
console.log(arr);  //[-2,-1,2,7,8,9,5]
result3 = arr.length;
console.log(result3);  //7
result4 = arr.splice(arr.length-1,1); //同pop
console.log(arr); //[-2,-1,2,7,8,9]
console.log(result4);  //[5]
arr.splice(arr.length,0,6,7);   //同push
console.log(arr);  //[-2,-1,2,7,8,9,6,7]
result5 = arr.length;
console.log(result5)  //8

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324826835&siteId=291194637