JavaScript 中最流行的数组方法,您不想错过!
时间:2025-01-23 18:01:14 397浏览 收藏
文章小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《JavaScript 中最流行的数组方法,您不想错过!》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
本文将详细讲解JavaScript中一些最常用的数组方法。
方法列表:
map()
filter()
reduce()
forEach()
find()
findIndex()
some()
every()
includes()
indexOf()
slice()
splice()
sort()
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学习网公众号吧!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
466 收藏
-
270 收藏
-
131 收藏
-
342 收藏
-
102 收藏
-
355 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习