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); // this is how to remove an item,
// ["Strawberry", "Mango"]
Remove items from an Index Position
var removedItems = fruits.splice(pos, n); // this is how to remove items, n defines the number of items to be removed,
// from that position onward to the end of array.
// let, n = 1;
// ["Strawberry"]
Copy an Array
var shallowCopy = fruits.slice(); // this is how to make a copy
// ["Strawberry"]
Syntax
[element0, element1, ..., elementN]
new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)
Description
js数组是一种list-like对象,它的原型方法可以进行遍历和修改操作。数组包含的元素个数和类型都不是固定的。
Methods
Array.from()
通过一个类数组对象或者可遍历对象创建数组
Array.isArray()
判断变量是否是Array的实例
Array.of()
通过数量类型不定的一组参数创建一个新的数组实例(与new Array或者Array有区别)
Polyfill 兼容实现
if(!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}
Array instance Methods
以下都是数组的修改方法
Array.prototype.copyWithin()
The copyWithin() method shallow copies part of an array to another location in the same array and returns it, without modifying its size.
arr.copyWithin(target[, start[, end]])
若target, start, end为负值,将被处理为arr.length
target. Zero based index at which to copy the sequence to.
Zero based index at which to start copying elements from
Zero based index at which to end copying elements from.
Array.prototype.fill()
The fill() method fills all the elements of an array from a start index to an end index with a static value.
arr.fill(value[, start = 0[, end = this.length]])
[1, 2, 3].fill(4); // [4, 4, 4]
[1, 2, 3].fill(4, 1); // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2); // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1); // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2); // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN); // [1, 2, 3]
Array(3).fill(4); // [4, 4, 4]
[].fill.call({ length: 3 }, 4); // {0: 4, 1: 4, 2: 4, length: 3}
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.sort()
arr.sort([compareFunction])
compareFunction可选。如果没有指定compareFunction,会试图把元素转换为字符串,并比较它们的unicode code point值.
Array.prototype.splice()
Array.prototype.unshift()
以下方法并不会修改数组,但会返回一些数组的元素
Array.prototype.concat()
Array.prototype.join()
Array.prototype.slice()
Array.prototype.toString()
Array.prototype.toLocaleString()
Array.prototype.indexOf()
Array.prototype.lastIndexOf()
以下是遍历方法
Array.prototype.forEach()
arr.forEach(callback[, thisArg])
callback参数 currentValue, index, array
thisArray可选。调用callback时的this
本方法不会遍历已被删掉的元素或者没有初始化的元素(稀疏数组)
不像map(), reduce(),本方法返回undefined,因此它不能链式调用.
Array.prototype.entries()
arr.entries()
返回一个Array iterator对象
var arr = ['a', 'b', 'c'];
var eArr = arr.entries();
console.log(eArr.next().value); // [0, 'a']
console.log(eArr.next().value); // [1, 'b']
console.log(eArr.next().value); // [2, 'c']
var arr = ['a', 'b', 'c'];
var eArr = arr.entries();
for (let e of eArr) {
console.log(e);
}
Array.prototype.every()
arr.every(callback[, thisArg])
被删除的元素不会被访问
判断每一个元素是否通过提供的function
callback回调参数: currentValue(必须) index(可选) array(可选)
thisAr可选。执行回调时的this
每一次回调都返回truthy value时,该函数返回true,否则false
var arr = [1, 2, 3, , , 6];
arr.every(function(value) {
if(value > 0) {
return true;
} else {
return false;
}
}); //true
Array.prototype.some()
Array.prototype.filter()
var new_array = arr.filter(callback[, thisArg])
被删除的元素不会被访问
创建一个新数组,它包含所有通过指定函数的元素
var arr = [1, 2, 3, , , 6];
arr.filter(function(value) {
if(value > 2) {
return true;
}
}); // 3, 6
Array.prototype.find()
arr.find(callback[, thisArg])
被删除的元素不会被访问
如果数组中的一个元素能通过指定函数,则立即返回该值
类似地, findIndex返回元素的下标
var arr = [1, 2, 3, , , 6];
arr.find(function(value) {
if(value > 2) {
return true;
}
}); // 3
Array.prototype.findIndex()
Array.prototype.keys()
arr.keys()
返回一个Array iterator对象
Basic usage
var arr = ["a", "b", "c"];
var iterator = arr.keys();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
Key iterator doesn't ignore holes
var arr = ["a", , "c"];
var sparseKeys = Object.keys(arr);
var denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]
Array.prototype.map()
var new_array = arr.map(callback[, thisArg])
被删除的元素也会被访问
根据指定方法返回的结果创建一个新数组.
var arr = [1, 2, 3, , , 6];
arr.map(function(value, index) {
return {
index: index,
value: value
}
});
/*[ { index: 0, value: 1 },
{ index: 1, value: 2 },
{ index: 2, value: 3 },
,
,
{ index: 5, value: 6 } ]*/
Array.prototype.reduce()
arr.reduceRight(callback[, initialValue])
将函数返回值作为一个叠加器,从左往右,依次叠加,直到只有一个值
callback回调参数previousValue, currentValue, index, array
initialValue可选,第一次调用callback时的previousValues值
var arr = [1, 2, 3, , , 6];
arr.reduce(function(prev, cur) {
return prev + cur;
}); // 12