1. arr.shift()
删除原数组第一项,并返回删除元素的值
如果数组为空则返回undefined。例如:
const nums = [1, 2, 3, 4];
const first = nums.shift();
console.log(nums);
console.log(first);
输出:
[2, 3, 4]
1
2. arr.unshift(n1,n2,...)
将参数添加到原数组开头,并返回数组的长度
注:IE6.0及以下返回值总为undefined。例如:
const nums = [1, 2, 3];
nums.unshift(4, 5);
console.log(nums);
输出:
[4, 5, 1, 2, 3]
3. arr.pop()
删除原数组最后一项,并返回删除元素的值
如果数组为空则返回undefined。例如:
let nums = [1, 2, 3, 4, 5, 6]
const lastNum = nums.pop()
console.log(`Removed ${lastNum}, now the numbers are ${nums}`)
输出:
Removed 6, now the numbers are 1,2,3,4,5
4. arr.push()
将参数添加到原数组末尾,并返回数组的长度。例如:
let nums = [1, 2, 3]
nums.push(4)
nums.push(5, 6)
console.log(nums)
输出:
[1, 2, 3, 4, 5, 6]
5. arr.concat([...])
返回一个新数组,新数组是由原数组的副本和参数链接而成的,故原数组不变
const num1 = [1,2,3];
const num2 = [4,5,6];
const merged = num1.concat(num2);
console.log(merged)
6. arr.splice(start,deleteCount,val1,val2,...)
从start位置开始删除deleteCount项,并从该位置起插入val1,val2,...
7. arr.reverse()
将数组反序
const nums = [1, 2, 3, 4]
const reversed = nums.reverse()
console.log(reversed)
输出:
[4, 3, 2, 1]
8. arr.sort(sortby)
按指定的参数对数组进行排序
1)默认排序顺序是根据字符串Unicode大小
2)参数sortby可选,规定排序顺序,必须是函数
3)如果想按照自定义标准进行排序,就需要提供比较函数,该函数要比较两个值,然后返回一个用于说明这两个值的相对顺序的数字。比较函数应该具有两个参数 a 和 b,其返回值如下:
若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。
若 a 等于 b,则返回 0。 若 a 大于 b,则返回一个大于 0 的值。
如:按.sort()原样使用方法对字符串数组进行排序:
const fruits = ["Banana", "Apple", "Clementine"]
const sorted = fruits.sort()
console.log(sorted)
输出:
["Apple", "Banana", "Clementine"]
例如,让我们按升序对数字列表进行排序:
const nums = [1, 2, 3, 4]
const sorted = nums.sort((a, b) => a - b)
console.log(sorted)
9. arr.slice(start,end)
返回从原数组中指定开始下标到结束下标之间的项组成的新数组(不包含结束那一项)
10. arr.join(separator)
将数组的元素组起一个字符串,以separator为分隔符
省略的话则用默认用逗号为分隔符。例如:
const words = ["This", "is", "a test"];
const sentence = words.join(" "); // separate words by blank spaces
console.log(sentence)
输出:
This is a test
11. arr.indexOf(a)
返回a元素索引,不存在返回-1
索引从0开始
12. arr.reduce(callback,[initialValue])
callback (执行数组中每个值的函数,包含四个参数)
previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue))
currentValue (数组中当前被处理的元素)
index (当前元素在数组中的索引)
array (调用 reduce 的数组)
initialValue (作为第一次调用 callback 的第一个参数。)
方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值。reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素
可以利用此函数对数组所有元素进行求和
sum = arr.reduce((a,b)=>a+b,0); // a为上次计算返回的值,b为当前累加的数组的元素,0表示初始累加值为0
例如,让我们计算数组中数字的总和:
const nums = [1,2,3,4,5,6];
const numSum = nums.reduce((sum, num) => sum + num);
console.log(numSum);
输出:
21
13. Array.forEach()
正如其名称.forEach()表明,它被用于执行一个动作的阵列的每个元件。例如,让我们打印数组的所有数字:
const nums = [1, 2, 3];
nums.forEach( num => console.log(num) );
输出:
1
2
3
14. Array.map()
你可以使用.map()method 对每个元素执行一个操作(即运行一个函数)并将结果放入一个新数组中。
例如,让我们将一个数字数组转换为一个新的平方数数组:
const nums = [1,2,3,4,5];
const squaredNums = nums.map( number => number * number );
console.log(squaredNums);
输出:
[1, 4, 9, 16, 25]
15. Array.filter()
顾名思义,你可以根据条件过滤数组的元素。结果,在过滤元素所在的位置创建了一个新数组。
例如,让我们从数字列表中过滤所有偶数:
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evenNums = nums.filter( num => num % 2 == 0);
console.log(evenNums);
输出:
[2, 4, 6, 8, 10]
16. Array.every()
你可以使用.every()method 来检查数组的每个元素是否都通过了条件。如果有,则该方法返回true,否则返回false。
例如,让我们检查是否所有人都饿了:
const moods = ["hungry", "hungry", "hungry"];
const allHungry = moods.every(mood => mood === "hungry");
console.log(allHungry);
输出:
true
17. Array.some()
该.some()方法与前面的.every()方法类似。不同之处在于,如果数组中的一个或多个元素满足条件,则.some()返回true,false否则返回。
例如,让我们检查是否所有人都饿了:
const moods = ["hungry", "sleepy", "thirsty"];
const someHungry = moods.some(mood => mood === "hungry");
console.log(someHungry);
输出:
true
ES6新增
1. arr = Array.from({...})
将对象转为真正的数组,这个对象有两种类型:类似数组的对象和可遍历的对象(如ES6中的Set和Map)
2. arr = Array.of()
用于将一组值,转换为数组
例:Array.of(3,11,8) // [3,11,8]
弥补数组构造函数Array()的不足
new Array(3) // [undefined*3]
Array.of可以替代Array()或者new Array()
Array.of() // []
Array.of(undefined) // [undefined]
Array.of(1) // [1]
Array.of(1,2) // [1,2]
arr.copyWidthin(target, start = 0, end = this.length)
将特定位置的成员复制到其他位置,然后返回当前数组
target(必选):从该位置开始替换数据
start(可选):从该位置开始读取数据,默认为0,如果是负数,表示倒数。
end(可选):到该位置前停止读取数据,默认等于数组长度。如果是负数,表示倒数。
3. arr.find()
用于找到第一个符合条件的数组成员。例如:
const cars = [
{brand: "Porsche", price: 105000},
{brand: "BMW", price: 32000},
{brand: "Skoda", price: 15000},
{brand: "Toyota", price: 11500}
];
const affordableCar = cars.find(car => car.price <= 20000);
console.log(affordableCar
输出:
{
brand:"Skoda",
price:15000
}
4. arr.findIndex()
返回第一个符合条件的数组成员位置。例如:
const nums = [1,2,3,3,3,2,1]
const idx = nums.findIndex( num => num == 3 )
console.log(idx)
输出:
2
5. Array.flat()
当你有一个多维数组时,你可以使用该.flat()方法将其展平。例如:
const nums = [0,1,2,[3,4]];
console.log(nums.flat());
输出:
[0, 1, 2, 3, 4]
你还可以指定要将数组维度压缩到的深度。例如,让我们将这个 4 维数组展平为 2 维:
const nums = [0, 1, 2, [[[[[3, 4]]]]]];
console.log(nums.flat(2));
输出:
[0,1,2,[3,4]]