A, array of various operations, usually we do not use much, but sometimes encountered in interviews, sometimes forget really embarrassed, so summary, occasionally look over, review is also good.
1.Array method toString()var fruits = ["Banana"."Orange"."Apple"."Mango"];
document.getElementById("demo").innerHTML = fruits.toString(); ToString is the most comfortable way to convert an array to a string.Copy the code
2.The join() method of the array method allows you to customize the separatorvar fruits = ["Banana"."Orange"."Apple"."Mango"];
document.getElementById("demo").innerHTML = fruits.join("*");
Copy the code
3.The pop() method of the array removes the last element in the array and returns the deleted valuevar fruits = ["Banana"."Orange"."Apple"."Mango"];
fruits.pop();
var x = fruits.pop(); // the x value is "Mango"
Copy the code
4.The array method push() adds a new element to the array and returns the length of the new arrayvar fruits = ["Banana"."Orange"."Apple"."Mango"];
var x = fruits.push("Kiwi"); // x is 5
Copy the code
5.The shift() method removes the first array element and returns the deleted element. The group of elements is changedvar fruits = ["Banana"."Orange"."Apple"."Mango"];
fruits.shift(); Remove the first element "Banana" from fruits
Copy the code
6.Unshift () of the array method adds an element to the array header and returns the length of the new arrayvar fruits = ["Banana"."Orange"."Apple"."Mango"];
fruits.unshift(); / / return 5
Copy the code
7.The array method splice(), which can be used to add the first argument of a new item to an array (2) defines where new elements should be added (concatenation). The second parameter (0) defines how many elements should be deleted. The remaining parameters (" Lemon ", "Kiwi") define the new elements to be added.Copy the code
8.The splice() method returns an array of deleted items:var fruits = ["Banana"."Orange"."Apple"."Mango"];
fruits.splice(2.2."Lemon"."Kiwi");
Copy the code
9.Use splice() to remove the element's first argument (0Defines where new elements should be added (added). The second parameter (1) defines that multiple elements should be deleted. The remaining parameters are omitted. No new elements will be added.var fruits = ["Banana"."Orange"."Apple"."Mango"];
fruits.splice(0.1); // Remove the first element of fruits
Copy the code
10.The concat() method creates a new array by merging (joining) existing arraysvar myGirls = ["Cecilie"."Lone"];
var myBoys = ["Emil"."Tobias"."Linus"];
var myChildren = myGirls.concat(myBoys); // connect myGirls to myBoys
Copy the code
11.The slice() method slices the new array with a fragment of the array, returning the new array without changing the old onevar fruits = ["Banana"."Orange"."Lemon"."Apple"."Mango"];
var citrus = fruits.slice(1); The slice() method creates a new array. It does not remove any elements from the source array.var fruits = ["Banana"."Orange"."Lemon"."Apple"."Mango"];
var citrus = fruits.slice(3); From the position3Begins excising parts of the array, excluding positions3Slice () accepts two arguments, such as (1.3). The method picks elements from the start argument until the end argument (not included).var fruits = ["Banana"."Orange"."Lemon"."Apple"."Mango"];
var citrus = fruits.slice(1.3);
Copy the code
Second, the next disk will look at other array manipulation methods, which will be more interesting and useful to use.