-
arr.push(element1, ..., elementN)
push()方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。
-
arr.pop()
pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。
-
arr.shift()
shift()方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
-
arr.unshift(element1, ..., elementN)
unshift()方法将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)。
-
arr.slice([begin[, end]])
slice()方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。
-
let new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
concat()方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
-
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
splice()方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
-
arr.every(callback(element[, index[, array]])[, thisArg])
every()方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。
-
arr.some(callback(element[, index[, array]])[, thisArg])
some()方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。
-
let newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
filter()方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
-
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);
map()方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。
-
const values = [1, 3, 4, 6, 7, 4, 3, 1]
values.forEach((item, index, array) => {
array[index] = item * 2
})
console.log(values)
forEach()方法对数组的每个元素执行一次给定的函数。
-
join & split
const colors = 'green, red, black'
const colorsArr = colors.split(',')
console.log(colorsArr)
const colorsStr = colorsArr.join(',')
console.log(colorsStr)
-
reverse & sort
const values = [1, 3, 44, 43, 654, 0];
values.reverse();
console.log(values); // [ 0, 654, 43, 44, 3, 1 ]
values.sort();
console.log(values); // [ 0, 1, 3, 43, 44, 654 ] 首字母开始比较
values.sort((val1, val2) => val2 - val1);
console.log(values); // [ 654, 44, 43, 3, 1, 0 ]
-
indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。
-
arr.lastIndexOf(searchElement[, fromIndex])
lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。
-
reduce & reduceRight
const values = [1, 3, 4, 4, 4, 9]
const sum = values.reduce((prev, cur, index, array) => {
return prev + cur
})
console.log(sum)
const sumRight = values.reduceRight((prev, cur, index, array) => {
return prev + cur
})
console.log(sumRight)
-
拓展运算符...
const colors = ['green', 'red', 'pink']
const colors1 = ['white', 'grey']
const colors2 = [...colors, ...colors1]
console.log(colors2)
-
Array.from() 方法对一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。
console.log(Array.from('foo'));
console.log(Array.from([1, 2, 3], x => x + x));
-
Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。
console.log(Array.of(undefined, 1, null));
-
copyWithin(target, start = 0, end = this.length)
const arr = [1, 3, 4, 4, 5, 8, 10, 1, 0].copyWithin(0, 3, 4)
console.log(arr)
const arr = [1, 3, 4, 4, 5, 8, 10, 1, 0].copyWithin(0, 3, 8)
console.log(arr1)
-
fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。
const colors = ['green', 'red', 'pink']
const colors1 = colors.fill('white')
console.log(colors1)
-
find & findIndex
const values = [1, 3, 4, 5, 6, NaN];
const findResult = values.find(num => num > 4 );
console.log(findResult);
const findIndexResult = values.findIndex(num => num > 4 );
console.log(findIndexResult);
-
entries(), keys() & values()
const colors = ["red", "green", "blue"];
for (const index of colors.keys()) {
console.log(index);
}
for (const ele of colors.values()) {
console.log(ele);
}
for (const [index, ele] of colors.entries()) {
console.log(index, ele);
}