JavaScript array of power hidden in the array method.
The array to a string toString ()
JavaScript method toString () converts the array is an array of values (comma delimited) string.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.toString()); //Banana,Orange,Apple,Mango
All JavaScript objects have toString () method.
join () method all array elements can also be incorporated into a string.
Its behavior is similar to toString (), but you can also specify delimiters:
var fruits = ["Banana", "Orange","Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
//结果
Banana * Orange * Apple * Mango
Popping 和 Pushing
In dealing with the array, remove elements and add new elements is very simple.
Popping and Pushing means:
from the array pop project, or to push an array of projects.
- 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 添加一个新元素
console.log(fruits); //Banana,Orange,Apple,Mango,Kiwi
- push () method returns the length of the new array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.push("Kiwi"); // x 的值是 5
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"
console.log(fruits); //Orange,Apple,Mango,Kiwi
- It shifts () 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
Change Element
: By using their index number to access the array element
array index (subscript) begin to 0. [0] is the first element of the array, [1] is the second, [2] is the third ...
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[0] = "Kiwi"; // 把 fruits 的第一个元素改为 "Kiwi"
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"
Removing elements
Since JavaScript array of objects belonging to which elements you can use the JavaScript delete operator to delete:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0]; // 把 fruits 中的首个元素改为 undefined
Use delete the array will leave undefined empty. Please use pop () or shift () instead.
Splicing array
splice () method can be used to add a new item to the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
console.log(fruits); //Banana,Orange,Lemon,Kiwi,Apple,Mango
//第一个参数(2)定义了应添加新元素的位置(拼接)。
//第二个参数(0)定义应删除多少元素。
//其余参数(“Lemon”,“Kiwi”)定义要添加的新元素。
splice () method returns an array containing the deleted items:
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 2, "Lemon", "Kiwi"); console.log(fruits); //Apple,Mango
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 中的第一个元素
console.log(fruits); //Orange,Apple,Mango
//第一个参数(0)定义新元素应该被添加(接入)的位置。
//第二个参数(1)定义应该删除多个元素。
//其余参数被省略。没有新元素将被添加。
Combining (connecting) array concat ()
concat () method to create a new array by combining (connecting) existing array:
var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias", "Linus"];
var myChildren = myGirls.concat(myBoys); // 连接 myGirls 和 myBoys
console.log(myChildren); //Cecilie,Lone,Emil,Tobias,Linus
concat () method does not change the existing array. It always returns a new array.
concat () method can be used any number of array parameters:
//合并3个数组
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.
The present embodiment from the array element 1 ( "Orange") starts cutting a section 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.
The present embodiment from the array element 3 ( "Apple") starts cutting a section of the array:
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(3); //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); //Orange,Lemon