JS 数组常用的方法(个人感觉)
1. forEach()
循环,无法在中间停止
2. some()
循环,找到符合条件的之后,可以通过 return true 退出循环
3. every()
测试数组中的所有元素是否都能通过某个指定函数的测试。返回一个布尔值。
1 2 3 4 5 6 7 8
| const arr = [-1, 3, 4, 5, 6];
let result = arr.every((value) => value > 0); console.log(result);
arr[0] = 1; result = arr.every((value) => value > 0); console.log(result);
|
4. filter()
创建一个新数组,它包含符合某个指定函数的测试的所有元素
5. reduce()
实现累加
arr.reduce((累加的结果, 当前循环项) => {}, 初始值)
1 2 3 4
| const arr = [1, 3, 7];
let result = arr.reduce((sum, value) => (sum += value), 0); console.log(result);
|
搭配 filter()实现,累加选中状态的总价格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| const arr = [ { id: 1, name: "西瓜", state: true, price: 10, count: 1, }, { id: 2, name: "榴莲", state: false, price: 80, count: 2, }, { id: 3, name: "草莓", state: true, price: 20, count: 3, }, ];
const amount = arr .filter((item) => item.state) .reduce((amount, item) => (amount += item.price * item.count), 0); console.log(amount);
|
6. map()
map()方法把调用它的数组的每一个元素分别传给指定的函数,返回这个函数的返回值构成的数组
1 2 3 4 5 6 7
| let a = [1, 2, 3]; let newA = a.map((v) => v * v); console.log(newA);
let a = [1, , 2, , 3]; let newA = a.map((v) => v * v); console.log(newA);
|
7. find()
遍历数组,寻找第一个符合条件的元素,找到就停止迭代
1 2 3
| let a = (a = [1, 2, 3, 4, 5]); console.log(a.find((v) => v >= 2)); console.log(a.find((v) => v < 0));
|
8. flat()
用于打平数组(把嵌套数组变为普通的数组元素)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let a = [1, [2, 3]]; console.log(a.flat());
a = [1, [2, [3]]]; console.log(a.flat());
a = [1, [2, [3, [4]]]]; console.log(a.flat(1)); console.log(a.flat(2)); console.log(a.flat(3)); console.log(a.flat(4));
a = [1, [2, 3, [4, 5, 6, [7, 8, [9, 10, [11, 12]]]]]]; console.log(a.flat(Infinity));
|
9. concat()
1 2 3 4
| let a = [1, 2, 3]; let b = [4, 5, 6]; console.log(b.concat(a)); console.log(b.concat(1, 2));
|
10. push()、pop()、shift()、unshift()
1 2 3 4 5 6 7 8 9 10 11 12 13
| let a = []; a.push(1); console.log(a.push(2, 3)); console.log(a);
console.log(a.pop()); console.log(a);
console.log(a.unshift(11, 22)); console.log(a);
console.log(a.shift()); console.log(a);
|
11. slice()、splice()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| let a = [1, 2, 3, 4, 5, 6, 7, 8]; console.log(a.slice(3, 6)); console.log(a);
console.log(a.slice(2));
console.log(a.slice(-4, -2));
console.log(a.slice(6, 0));
a = [1, 2, 3, 4, 5, 6, 7, 8]; console.log(a.splice(2, 3, "Hello", "Hi")); console.log(a);
console.log(a.slice(3));
|
12. indexOf()、lastIndexOf()
1 2 3 4 5 6 7
| let a = [1, 2, 3, 4, 2, 1]; console.log(a.indexOf(2)); console.log(a.lastIndexOf(2)); console.log(a.lastIndexOf(99));
console.log(a.indexOf(2, 2)); console.log(a.lastIndexOf(2, -3));
|
13. includes()
1 2 3
| let a = [1, "Hello", true]; console.log(a.includes(true)); console.log(a.includes(2));
|
14. fill()
1 2 3 4
| let a = new Array(7); console.log(a.fill(0)); console.log(a.fill(7, 2)); console.log(a.fill(3, 2, -2));
|
15. join()、split()
1 2 3 4 5 6 7 8 9 10
| let a = [1, 2, 3]; console.log(a.join()); console.log(a.join(" "));
a = "Hello world!"; console.log(a.split("")); a = "Hello, world!"; console.log(a.split(","));
|
16. sort()
sort()方法对数组元素按字母顺序对数组元素排序
1 2
| let arr = [1, 2, 11, 23, 22, 111, 12, 9, 8]; console.log(arr.sort());
|
结果:
可以传入一个回调函数,来自定义排序规则。
回调函数的格式是
1 2 3 4
| (a, b) => { return xxx; };
|
当返回值大于 0 时,a 排在 b 的后面;
当返回值小于 0 时,a 排在 b 的前面;
当返回值等于 0 时,a 和 b 的顺序不改变。
所以,要实现升序排序,可以按下面的方法
1 2 3 4 5 6 7 8 9 10 11 12
| let arr = [1, 2, 11, 23, 22, 111, 12, 9, 8]; console.log( arr.sort((a, b) => { if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } }) );
|
上面的代码还可以简化为:
1 2 3
| let arr = [1, 2, 11, 23, 22, 111, 12, 9, 8]; console.log(arr.sort((a, b) => a - b));
|
原因(看不懂的话,结合上面红字):
当 a > b 时,
a - b > 0, return a - b 排序的结果 ==> b, a (升序)
b - a < 0, return b - a 排序的结果 ==> a, b (降序)
当 a < b 时,
a - b < 0, return a - b 排序的结果 ==> a, b (升序)
b - a > 0, return b - a 排序的结果 ==> b, a (降序)
当 a = b 时,
a - b = b - a = 0, return a - b 和 return b - a 排序结果都不变
从上面所有的情况,可以发现,无论 a > b,还是 a < b, return a - b 总能得到升序的结果,return b - a 总能得到降序的结果