array method

 

push() and pop()

The push() and pop() methods allow to use the .push() method to add one or more elements to the end of the array using the array as a stack and return the new length of the array. The pop() method does the opposite: it removes the last element of the array, reduces the array length and returns the value it removed

E.g

var arr = []; arr: []

arr.push(1,2);      arr:[1,2]     

arr.pop();            arr:[1]          

arr.push(3)          arr:[1,3]    

join() 

array.join() converts all the elements in the array into strings and joins them together, returning the resulting string

You can specify an optional string in a string to separate the elements of the array, if you don't specify a separator, use a comma to separate

E.g:

var a=[1,2,3]

a.join();    //  "1,2,3"

a.join("  "):   //  "1 2 3"

a.join("");  //  "123"

sort()

array.sort() sorts the elements in an array and returns the sorted array When sort() is called with no arguments, the array elements are sorted alphabetically

var a=new array("banana","cherry","apple");

a.sort();

var s=a.join(", ")    // s=="apple, banana, cheryy"

 

concat()

array.concat() creates and returns a new array whose elements include the elements of the original array on which concat() was called and each argument to concat()

vara = [1,2,3];

a.concat(4,5) //return [1,2,3,4,5]

a.concat([4,5]) //return [1,2,3,4,5]

a.concat([4,5],[6,7]) //return [1,2,3,4,5,6,7]

a.concat(4,[5,[6,7]]) //return [1,2,3,4,5,[6,7]]

slice()

array.slice() returns a slice or subarray of the specified array. Its two parameters specify where the fragment begins and ends, respectively. The returned array contains all array elements between the position specified by the first parameter and all to but not the position specified by the second parameter

If only one argument is specified, the returned array will contain all elements from the beginning to the end of the array.

If a negative number appears in the argument, it represents the position relative to the last element in the array.

var a=[1,2,3,4,5];

a.slice(0,3); //return [1,2,3]

a.slice(3); //return [4,5]

a.slice(1,-1) //return [2,3,4]

a.slice(-3,-2) //return [3]

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939861&siteId=291194637