- Array.prototype.push
- Array.prototype.pop
- Array.prototype.unshift
- Array.prototype.shift
- Array.prototype.splice
- Array.prototype.slice
- Array.prototype.concat
- Array.prototype.indexOf
- Array.prototype.includes
- Array.prototype.forEach
- Array.prototype.find
- Array.prototype.filter
- Array.prototype.map
- Array.prototype.some
- Array.prototype.every
- Array.prototype.reduce
- Array.prototype.sort
- Array.prototype.reverse
- Array.prototype.split
- Array.prototype.join
- Array.isArray
- Array.from
Array.prototype.push
const colors = ['Red', 'Green', 'Blue'];
colors.push('Yellow');
console.log(colors); // ['Red', 'Green', 'Blue', 'Yellow']
Array.prototype.pop
const colors = ['Red', 'Green', 'Blue'];
const item = colors.pop();
console.log(item, colors); // 'Blue' ['Red', 'Green']
Array.prototype.unshift
Array.prototype.shift
Array.prototype.splice
返回被删除的元素组成的数组
const colors = ['Red', 'Green', 'Blue'];
// 正常使用
colors.splice(1, 1, 'Yellow', 'Orange'); // ['Red', 'Yellow', 'Orange', 'Blue']
// 删除元素
colors.splice(1, 1); // ['Red', 'Blue']
Array.prototype.slice
Array.prototype.concat
Array.prototype.indexOf
Array.prototype.includes
Array.prototype.forEach
Array.prototype.find
Array.prototype.filter
Array.prototype.map
Array.prototype.some
Array.prototype.every
Array.prototype.reduce
const nums = [1, 2, 3, 4, 5];
nums.reduce((sum, current) => {
return sum + current;
}, 0)
Array.prototype.sort
Array.prototype.reverse
Array.prototype.split
Array.prototype.join
Array.isArray
Array.from
该方法接受对象,检查它是一个可迭代对象或类数组对象,然后创建一个新数组,并将该对象的所有元素复制到这个新数组。
// 类数组
let arrayLike = {
0: 0,
1: 1,
length: 2,
}
let arr = Array.from(arrayLike)
console.log(arr.pop())
// 可迭代对象
let range = {
from: 1,
to: 5,
}
range[Symbol.iterator] = function () {
return {
current: this.from,
last: this.to,
next() {
if (this.current <= this.last) {
return { done: false, value: this.current++ }
} else {
return { done: true }
}
},
}
}
let arr = Array.from(range)
console.log(arr) // [1, 2, 3, 4, 5]
// mapFn
let arr = Array.from(range, item => item * 2)
console.log(arr) // [2, 4, 6, 8, 10]