Array Methods
The array to a string
JavaScript method toString () converts the array is an array of values (comma delimited) string.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString(); //Banana,Orange,Apple,Mango
join () method all array elements can also be incorporated into a string.
Its behavior is similar to toString (), but you also can specify delimiters
var fruits = ["Banana", "Orange","Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * "); //Banana * Orange * Apple * Mango
Add elements or remove elements
pop () method removes the last element from an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); // 从 fruits 删除最后一个元素("Mango")
pop () method returns the "ejected" values:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.pop(); // x 的值是 "Mango"
push () method (at the end of the array) to add a new element to the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); // 向 fruits 添加一个新元素
push () method returns the length of the new array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.push("Kiwi"); // x 的值是 5
The length property provides an easy way to add a new element to an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Kiwi"; // 向 fruits 追加 "Kiwi"
Displacement element
Displacement and pop equivalent, but the first processing element rather than the last one. shift () method removes the first element of the array, and all the other elements "shift" to a lower index.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift(); // 从 fruits 删除第一个元素 "Banana"
shift () method returns the string is "shifted out" in:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift(); // 返回 "Banana"
unshift () method (in the beginning) to add a new element to the array, and "reverse displacement" old elements
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon"); // 向 fruits 添加新元素 "Lemon"
unshift () method returns the length of the new array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon"); // 返回 5
Removing elements
Since JavaScript array of objects belonging to which elements you can use the JavaScript delete operator to deletevar fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0]; // 把 fruits 中的首个元素改为 undefined
⭐⭐⭐ array using the delete will leave undefined empty. Please use pop () or shift () instead. ⭐⭐⭐
Splicing array
splice () method can be used to add a new entry to an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi"); //Banana,Orange,Lemon,Kiwi,Apple,Mango
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. (Deleted from the beginning of the first parameter to the location)
remaining parameters ( "Lemon", "Kiwi" ) is defined to add new elements.
splice () method returns a * array that contains the deleted items
Use splice () to remove elements
By clever parameter settings, you can use the splice () Removes the element without leaving "holes" in the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(0, 1); // 删除 fruits 中的第一个元素
The first parameter a (0) to define a new element should be added position (Access).
The second parameter (1) definition should delete multiple elements.
The remaining parameters are omitted. No new elements will be added.
Combining (connecting) array
concat () method to create a new array by combining (connecting) existing array
var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Lone", "Tobias", "Linus"];
var myChildren = myGirls.concat(myBoys);
// 连接 myGirls 和 myBoys
//Cecilie,Lone,Lone,Tobias,Linus
concat () method does not change the existing array. It always returns a new array.
concat () _ The method may be used any number of array parameter _
var arr1 = ["Cecilie", "Lone"];
var arr2 = ["Emil", "Tobias", "Linus"];
var arr3 = ["Robin", "Morgan"];
var myChildren = arr1.concat(arr2, arr3); // 将arr1、arr2 与 arr3 连接在一起
concat () method may be used as the parameter value:
var arr1 = ["Cecilie", "Lone"];
var myChildren = arr1.concat(["Emil", "Tobias", "Linus"]);
Crop array
slice () method of cutting out a segment with a new array of the array.
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1);
//Orange,Lemon,Apple,Mango
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);
//citrus = ["Apple", "Mango"]
slice () two acceptable parameters, such as (1, 3).
This method starts from an element selected parameters, until the end of the parameter (not included) so far.
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
//citrus = ["Orange", "Lemon"]
If the end parameter is omitted, such as the first example, the slice () will cut the remaining portion of the array.
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(2);
//citrus = ["Lemon", "Apple", "Mango"]
Automatic toString ()
If you need the original value, the JavaScript will automatically convert to a string array. The following two examples will produce the same result:var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
All JavaScript objects have toString () method.