js array method

There are many JavaScript array on a portion of an article that

Now and then listed some of the

pop () method removes the last element from an array:

 

. 1  var fruits = [ "Banana", "Orange", "the Apple", "Mango" ];
 2 fruits.pop ();               // delete the last element ( "Mango") from the fruits

pop () method returns the "ejected" values:

1 var fruits = ["Banana", "Orange", "Apple", "Mango"];
2 var x = fruits.pop();      // x 的值是 "Mango"

push () method (at the end of the array) to add a new element to the array:

. 1  var fruits = [ "Banana", "Orange", "the Apple", "Mango" ];
 2 fruits.push ( "Kiwi");        //   add a new element to the fruits

push () method returns the length of the new array:

1 var fruits = ["Banana", "Orange", "Apple", "Mango"];
2 var x =  fruits.push("Kiwi");   //  x 的值是 5

 

Change Element to access array elements by using their index number:

Array index (subscript) begin to 0. [0] is the first element of the array, [1] is the second, [2] is the third ...

. 1  var fruits = [ "Banana", "Orange", "the Apple", "Mango" ];
 2 fruits [0] = "Kiwi";         // the fruits of the first element to "Kiwi"

The length property provides an easy way to add a new element to an array:

1 var fruits = ["Banana", "Orange", "Apple", "Mango"];
2 fruits[fruits.length] = "Kiwi";          // 向 fruits 追加 "Kiwi"

concat () method to create a new array by combining (connecting) existing array:

1 var myGirls = ["Cecilie", "Lone"];
2 var myBoys = ["Emil", "Tobias", "Linus"];
3 var myChildren = myGirls.concat(myBoys);   // 连接 myGirls 和 myBoys

concat () method does not change the existing array. It always returns a new array.

concat () method can be used any number of array parameters:

. 1  var of arr1 = [ "Cecilie", "Lone" ];
 2  var arr2 is = [ "of Emil", "Tobias", "of Linus" ];
 . 3  var ARR3 = [ "Robin", "Morgan" ];
 . 4  var myChildren = arr1.concat (arr2 is, ARR3);    // connected together arr1, arr2 and ARR3

concat () method may be used as the parameter value:

1 var arr1 = ["Cecilie", "Lone"];
2 var myChildren = arr1.concat(["Emil", "Tobias", "Linus"]); 

splice () method can be used to add a new item to the array:

1 var fruits = ["Banana", "Orange", "Apple", "Mango"];
2 fruits.splice(2, 0, "Lemon", "Kiwi");

The first parameter (2) defines the location (splicing) the new element should be added.

The second parameter (0) define how many elements should be removed.

The remaining parameters ( "Lemon", "Kiwi") to add a new element definitions.

splice () method returns an array containing the deleted items:

1 var fruits = ["Banana", "Orange", "Apple", "Mango"];
2 fruits.splice(2, 2, "Lemon", "Kiwi");

 splice remove elements () without leaving "holes" in the array:

. 1  var fruits = [ "Banana", "Orange", "the Apple", "Mango" ];
 2 fruits.splice (0,. 1);         // delete the first element of fruits

The first parameter a (0) should be added to define a new position of the element (Access).

The second parameter (1) the definition of multiple elements should be removed.

The remaining parameters are omitted. No new elements will be added.

Guess you like

Origin www.cnblogs.com/qdjj/p/12386739.html