es6中迭代数组的方法有哪些

发布时间:2022-10-24 16:55:52 作者:iii
来源:亿速云 阅读:122

ES6中迭代数组的方法有哪些

在ES6(ECMAScript 2015)中,JavaScript引入了许多新的特性和方法,使得数组的操作更加方便和高效。本文将详细介绍ES6中用于迭代数组的各种方法,包括它们的用法、特点以及适用场景。

1. forEach 方法

forEach 是ES5中引入的数组迭代方法,但在ES6中仍然广泛使用。它允许你对数组中的每个元素执行一个回调函数。

1.1 基本用法

const array = [1, 2, 3, 4, 5];

array.forEach((element, index, array) => {
  console.log(`Element: ${element}, Index: ${index}, Array: ${array}`);
});

1.2 特点

1.3 适用场景

2. map 方法

map 方法创建一个新数组,其结果是该数组中的每个元素调用一次提供的回调函数后的返回值。

2.1 基本用法

const array = [1, 2, 3, 4, 5];

const newArray = array.map((element, index, array) => {
  return element * 2;
});

console.log(newArray); // [2, 4, 6, 8, 10]

2.2 特点

2.3 适用场景

3. filter 方法

filter 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。

3.1 基本用法

const array = [1, 2, 3, 4, 5];

const newArray = array.filter((element, index, array) => {
  return element % 2 === 0;
});

console.log(newArray); // [2, 4]

3.2 特点

3.3 适用场景

4. reduce 方法

reduce 方法对数组中的每个元素执行一个由你提供的 reducer 函数(升序执行),将其结果汇总为单个返回值。

4.1 基本用法

const array = [1, 2, 3, 4, 5];

const sum = array.reduce((accumulator, currentValue, index, array) => {
  return accumulator + currentValue;
}, 0);

console.log(sum); // 15

4.2 特点

4.3 适用场景

5. reduceRight 方法

reduceRight 方法与 reduce 类似,但它从数组的末尾开始向前执行。

5.1 基本用法

const array = [1, 2, 3, 4, 5];

const sum = array.reduceRight((accumulator, currentValue, index, array) => {
  return accumulator + currentValue;
}, 0);

console.log(sum); // 15

5.2 特点

5.3 适用场景

6. every 方法

every 方法测试数组中的所有元素是否都通过了指定函数的测试。

6.1 基本用法

const array = [1, 2, 3, 4, 5];

const allEven = array.every((element, index, array) => {
  return element % 2 === 0;
});

console.log(allEven); // false

6.2 特点

6.3 适用场景

7. some 方法

some 方法测试数组中是否至少有一个元素通过了指定函数的测试。

7.1 基本用法

const array = [1, 2, 3, 4, 5];

const hasEven = array.some((element, index, array) => {
  return element % 2 === 0;
});

console.log(hasEven); // true

7.2 特点

7.3 适用场景

8. find 方法

find 方法返回数组中满足提供的测试函数的第一个元素的值。如果没有找到,则返回 undefined

8.1 基本用法

const array = [1, 2, 3, 4, 5];

const found = array.find((element, index, array) => {
  return element > 3;
});

console.log(found); // 4

8.2 特点

8.3 适用场景

9. findIndex 方法

findIndex 方法返回数组中满足提供的测试函数的第一个元素的索引。如果没有找到,则返回 -1

9.1 基本用法

const array = [1, 2, 3, 4, 5];

const foundIndex = array.findIndex((element, index, array) => {
  return element > 3;
});

console.log(foundIndex); // 3

9.2 特点

9.3 适用场景

10. entries 方法

entries 方法返回一个新的数组迭代器对象,该对象包含数组中每个索引的键值对。

10.1 基本用法

const array = ['a', 'b', 'c'];

const iterator = array.entries();

for (const [index, element] of iterator) {
  console.log(`Index: ${index}, Element: ${element}`);
}

10.2 特点

10.3 适用场景

11. keys 方法

keys 方法返回一个新的数组迭代器对象,该对象包含数组中每个索引的键。

11.1 基本用法

const array = ['a', 'b', 'c'];

const iterator = array.keys();

for (const key of iterator) {
  console.log(`Key: ${key}`);
}

11.2 特点

11.3 适用场景

12. values 方法

values 方法返回一个新的数组迭代器对象,该对象包含数组中每个索引的值。

12.1 基本用法

const array = ['a', 'b', 'c'];

const iterator = array.values();

for (const value of iterator) {
  console.log(`Value: ${value}`);
}

12.2 特点

12.3 适用场景

13. Symbol.iterator 方法

Symbol.iterator 是ES6中引入的一个内置符号,用于定义对象的默认迭代器。数组默认实现了 Symbol.iterator 方法,因此可以直接使用 for...of 循环遍历数组。

13.1 基本用法

const array = ['a', 'b', 'c'];

for (const element of array) {
  console.log(`Element: ${element}`);
}

13.2 特点

13.3 适用场景

14. Array.from 方法

Array.from 方法从一个类似数组或可迭代对象创建一个新的数组实例。

14.1 基本用法

const arrayLike = { length: 3, 0: 'a', 1: 'b', 2: 'c' };

const array = Array.from(arrayLike);

console.log(array); // ['a', 'b', 'c']

14.2 特点

14.3 适用场景

15. Array.of 方法

Array.of 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。

15.1 基本用法

const array = Array.of(1, 2, 3, 4, 5);

console.log(array); // [1, 2, 3, 4, 5]

15.2 特点

15.3 适用场景

16. Array.prototype[@@iterator] 方法

Array.prototype[@@iterator] 是数组的默认迭代器方法,与 Symbol.iterator 相同。

16.1 基本用法

const array = ['a', 'b', 'c'];

const iterator = array[Symbol.iterator]();

for (const element of iterator) {
  console.log(`Element: ${element}`);
}

16.2 特点

16.3 适用场景

17. Array.prototype.includes 方法

Array.prototype.includes 方法判断一个数组是否包含一个指定的值,根据情况返回 truefalse

17.1 基本用法

const array = [1, 2, 3, 4, 5];

const includesThree = array.includes(3);

console.log(includesThree); // true

17.2 特点

17.3 适用场景

18. Array.prototype.flat 方法

Array.prototype.flat 方法创建一个新数组,其中所有子数组元素递归地连接到指定深度。

18.1 基本用法

const array = [1, [2, [3, [4, 5]]];

const flattened = array.flat(2);

console.log(flattened); // [1, 2, 3, [4, 5]]

18.2 特点

18.3 适用场景

19. Array.prototype.flatMap 方法

Array.prototype.flatMap 方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。

19.1 基本用法

const array = [1, 2, 3, 4, 5];

const flatMapped = array.flatMap((element) => [element * 2]);

console.log(flatMapped); // [2, 4, 6, 8, 10]

19.2 特点

19.3 适用场景

20. Array.prototype.copyWithin 方法

Array.prototype.copyWithin 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,而不修改其大小。

20.1 基本用法

const array = [1, 2, 3, 4, 5];

array.copyWithin(0, 3, 5);

console.log(array); // [4, 5, 3, 4, 5]

20.2 特点

20.3 适用场景

21. Array.prototype.fill 方法

Array.prototype.fill 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。

21.1 基本用法

const array = [1, 2, 3, 4, 5];

array.fill(0, 2, 4);

console.log(array); // [1, 2, 0, 0, 5]

21.2 特点

21.3 适用场景

22. Array.prototype.sort 方法

Array.prototype.sort 方法对数组的元素进行排序,并返回数组。

22.1 基本用法

const array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];

array.sort((a, b) => a - b);

console.log(array); // [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

22.2 特点

22.3 适用场景

23. Array.prototype.reverse

推荐阅读:
  1. javascript中es6 filter()数组过滤的方法有哪些
  2. JS中数组的迭代方法有哪些?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

es6

上一篇:nodejs支持es6吗

下一篇:es6中object新增了什么方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》