15个你应该知道的JavaScript的重要数组方法

开发 前端
我们会在构建的每个应用程序中使用数组。它有几种方法,其中一些非常令人困惑。我列出了 15 种,我们应该掌握的数组方法,因为它们经常派上用场。

数组方法的重要一点是有些是可变的,有些是不可变的。在决定针对特定问题使用哪种方法时,务必牢记这一点。

此列表中的大多数数组方法都采用类似的回调作为参数。第一个参数是当前项,第二个参数是索引,第三个是整个数组。现在我们已经解决了这个问题,让我们从列表开始:

1、ForEach

循环遍历数组中的每个元素并执行回调函数。

const arr = [1, 2, 3];
arr.forEach(num => console.log(num));
// Console: 1, 2, 3

2、Map

循环遍历数组中的每个元素并执行回调函数。使用回调函数的返回值创建一个新数组。

const arr = [1, 2, 3, 4, 5];
const areEven = arr.map(num => num % 2 === 0);
console.log(areEven); // Console: [false, true, false, true, false]

3、Filter

循环遍历数组中的每个元素,并仅选择符合条件的元素。根据所选元素返回一个新数组。

const arr = [1, 2, 3, 4, 5];
const evenNumbers = arr.filter(num => num % 2 === 0);
console.log(evenNumbers); // Console [2, 4]

4、Find

查找数组中满足条件的第一个元素。如果没有找到,将返回 undefined。

const arr = [1, 2, 3, 4, 5];
const firstEvenNumber = arr.find(num => num % 2 === 0);
console.log(firstEvenNumber); // Console [2]

5、FindIndex

与前面的方法类似,它返回满足给定条件的第一个元素的索引。如果没有找到,则返回 -1。

const arr = [1, 2, 3, 4, 5];
const firstEvenNumberIdx = arr.findIndex(num => num % 2 === 0);
console.log(firstEvenNumberIdx);

6、Reduce

这是一种高级方法,可用于组合数组的元素。主要区别在于回调将累加器作为第一个参数。回调的返回值成为下一次迭代的累加器。

const arr = [1, 2, 3, 4, 5];
// `acc` is the value of the accumulator
// the acccumulator is return value of the previous callback
// the second argument i.e `0` is the default value
const sum = arr.reduce((acc, num) => acc + num, 0);
console.log(sum); // Console: 15

7、Every

此方法接受一个返回布尔值的回调。如果条件对数组中的所有元素都有效,那么 Every() 将返回 true。

const arr = [1, 2, 3, 4, 5];
const areAllEven = arr.every(num => num % 2 === 0);
console.log(areAllEven); // Console: false

8、Some

像前面的方法一样,这个方法也接受一个返回布尔值的回调。如果条件对至少一个元素有效,Some() 将返回 true。

const arr = [1, 2, 3, 4, 5];
const isOneEven = arr.some(num % 2 === 0);
console.log(isOneEven); // true

9、 Sort

这是一种用于对数组中的元素进行排序的方法。

默认情况下,它按升序对数组进行排序。它需要一个回调函数,有两个元素——a 和 b。如果 a 小于 b,则返回 -1,否则返回 1。

如果它们相等,则返回 0。

const arr = [1, 2, 3, 4, 5];
const descendingArr = arr.sort((a, b) => b - a);
console.log(descendingArr);

请记住,与其他数组方法不同,sort 会改变数组。

10、Flat

Flat 用于将嵌套数组展平为单个数组。您可以指定将数组展平的深度。

const arr = [[[1, 2], [3]], [4, 5]];
const flattenedArr = arr.flat(4);
console.log(flattenedArr); // Console [1, 2, 3, 4, 5]

11、 Reverse

反转数组中元素的顺序。

const arr = [1, 2, 3, 4, 5];
const reversedArr = arr.reverse();
console.log(reversedArr); // Console [5, 4, 3, 2, 1]

12、Include

如果数组中存在元素,则此方法返回 true。

const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(5)); // true
console.log(arr.includes(10)); // false

13、Fill

fill 方法将数组的元素设置为给定值。当我想使用 map/forEach 方法特定次数时,我喜欢使用此方法。

const emptyArr = new Array(5);
// The problem with this is that you get `[empty x 10]`
// You need real values to map over it.
const filledArr = emptyArr.fill(3); // Console [3, 3, 3, 3, 3]

14、At

此方法返回给定索引的元素。这与访问(即 arr[1])元素的传统方式之间的区别在于它也支持负索引。

const arr = [1, 2, 3, 4, 5];
console.log(arr.at(1)); // 2
console.log(arr.at(-1)); // 5
// Important: Negative indices start from `1`, positive indices start from `0`.

15、 Concat

此方法用于组合两个数组。
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [6, 7, 8, 9, 10];
console.log(arr1.concat(arr2)); // Console [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

总结

以上就是我今天跟大家分享的15个关于JavaScript的重要数组的方法。

希望对你有帮助,如果你喜欢它的话,请记得分享给你身边做开发的朋友。

最后,感谢你的阅读,祝编程愉快!

责任编辑:庞桂玉 来源: web前端开发
相关推荐

2020-03-19 15:30:08

JavaScript数组字符串

2022-09-27 14:36:57

JavaScrip数组开发

2023-07-04 15:52:49

JavaScript数组

2022-10-10 23:14:40

JavaScrip对象属性

2022-10-18 16:35:51

JavaScrip数组参数

2022-11-13 15:33:30

JavaScript数组开发

2022-08-10 12:02:52

面试JavaScript

2023-05-08 16:06:33

2017-07-24 14:59:31

ERP软件连续性

2022-05-06 12:03:16

数组Javascript

2023-12-05 08:02:51

JavaScript字符串功能

2022-11-23 16:12:57

JavaScript数据类型数组

2022-04-18 12:42:44

Linux

2020-04-03 19:21:59

JavaScript编程语言开发

2024-03-21 14:27:13

JavaScript数组

2023-02-01 08:31:48

2019-08-13 16:23:19

JavaScript数组方法

2023-11-14 16:57:10

2019-07-25 10:08:05

JavaScript数组转换

2018-10-17 09:00:00

JavaScript工具前端
点赞
收藏

51CTO技术栈公众号