Array
Array对象是一个用于创建数组的全局对象。
create an Array
var fruits = ["Apple", "Banana"];
console.log(fruits.length);
// 2
Access (index into) an Array item
var first = fruits[0];
// Apple
var last = fruits[fruits.length - 1];
// Banana
Loop over an Array
fruits.forEach(function (item, index, array) {
console.log(item, index);
});
// Apple 0
// Banana 1
Add to the end of an Array
var newLength = fruits.push("Orange");
// ["Apple", "Banana", "Orange"]
Removed from the end of an Array
var last = fruits.pop(); // remove Orange (from the end)
// ["Apple", "Banana"];
Add to the front of an Array
var newLength = fruits.unshift("Strawberry") // add to the front
// ["Strawberry", "Banana"];
Find the index of an item in the Array
fruits.push("Mango");
// ["Strawberry", "Banana", "Mango"]
var pos = fruits.indexOf("Banana");
// 1
Remove an item by Index Position
var removedItem = fruits.splice(pos, 1); //