登录
首页 >  文章 >  前端

JavaScript 中最流行的数组方法,您不想错过!

时间:2025-01-23 18:01:14 397浏览 收藏

文章小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《JavaScript 中最流行的数组方法,您不想错过!》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


JavaScript 中最流行的数组方法,您不想错过!

本文将详细讲解JavaScript中一些最常用的数组方法。

方法列表:

  1. map()
  2. filter()
  3. reduce()
  4. forEach()
  5. find()
  6. findIndex()
  7. some()
  8. every()
  9. includes()
  10. indexOf()
  11. slice()
  12. splice()
  13. sort()
  14. join()

1. map()

map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。原始数组保持不变。

const numbers = [1, 2, 3];
const doubled = numbers.map(x => x * 2);                    // [2, 4, 6]
const users = [{ name: 'john' }, { name: 'jane' }];
const names = users.map(user => user.name);                 // ['john', 'jane']

2. filter()

filter() 方法创建一个新数组, 其中包含通过提供的函数测试的所有元素。原始数组保持不变。

const nums = [1, 2, 3, 4, 5, 6];
const evenNums = nums.filter(n => n % 2 === 0);            // [2, 4, 6]
const products = [{ price: 10 }, { price: 20 }];
const expensive = products.filter(p => p.price > 15);      // [{ price: 20 }]

3. reduce()

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

const sum = [1, 2, 3].reduce((acc, curr) => acc + curr, 0);    // 6
const cart = [{ price: 10 }, { price: 20 }];
const total = cart.reduce((sum, item) => sum + item.price, 0); // 30

4. forEach()

forEach() 方法对数组的每个元素执行一次提供的函数。它不返回值。

[1, 2, 3].forEach(n => console.log(n));
['a', 'b'].forEach((item, index) => console.log(`${index}: ${item}`));

5. find()

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

const users2 = [{ id: 1, name: 'john' }, { id: 2, name: 'jane' }];
const john = users2.find(user => user.name === 'john');    // { id: 1, name: 'john' }
const nums2 = [1, 2, 3, 4];
const firstEven = nums2.find(n => n % 2 === 0);           // 2

6. findIndex()

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

const fruits = ['apple', 'banana', 'cherry'];
const bananaIndex = fruits.findIndex(f => f === 'banana'); // 1
const userIndex = users2.findIndex(u => u.id === 2);      // 1

7. some()

some() 方法测试数组中的至少一个元素是否通过了提供的函数测试。

const hasEven = [1, 2, 3].some(n => n % 2 === 0);         // true
const hasExpensive = products.some(p => p.price > 15);     // true

8. every()

every() 方法测试数组中的所有元素是否都通过了提供的函数测试。

const allPositive = [1, 2, 3].every(n => n > 0);          // true
const allCheap = products.every(p => p.price < 15);       // false

9. includes()

includes() 方法确定数组是否包含一个特定值。

const numbers2 = [1, 2, 3];
const hasTwo = numbers2.includes(2);                       // true
const hasZero = numbers2.includes(0);                      // false

10. indexOf()

indexOf() 方法返回在数组中可以找到一个特定元素的第一个索引;如果找不到,则返回 -1。

const colors = ['red', 'blue', 'green'];
const blueIndex = colors.indexOf('blue');                  // 1
const yellowIndex = colors.indexOf('yellow');              // -1

11. slice()

slice() 方法返回一个新的数组对象,该对象包含从开始到结束(不包括结束)的选定元素。原始数组保持不变。

const arr = [1, 2, 3, 4, 5];
const middle = arr.slice(1, 4);                           // [2, 3, 4]
const last = arr.slice(-2);                               // [4, 5]

12. splice()

splice() 方法通过从数组中添加/删除元素来更改内容。此方法会修改原始数组。

const months = ['jan', 'march', 'april'];
months.splice(1, 0, 'feb');                               // ['jan', 'feb', 'march', 'april']
months.splice(2, 1);                                      // ['jan', 'feb', 'april']

13. sort()

sort() 方法对数组的元素进行排序,并返回数组。此方法会修改原始数组。

const unsorted = [3, 1, 4, 1, 5];
unsorted.sort((a, b) => a - b);                          // [1, 1, 3, 4, 5]
const names2 = ['zack', 'alice', 'bob'];
names2.sort();                                           // ['alice', 'bob', 'zack']

14. join()

join() 方法将数组元素连接成一个字符串。

const elements = ['Fire', 'Air', 'Water'];
const result = elements.join();                          // "Fire,Air,Water"
const result2 = elements.join(' - ');                    // "Fire - Air - Water"

希望以上解释能够帮助您更好地理解和使用这些JavaScript数组方法。

理论要掌握,实操不能落!以上关于《JavaScript 中最流行的数组方法,您不想错过!》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>