每个开发人员都应该掌握的 JavaScript 数组方法(第 1 部分)
最近发现不少小伙伴都对文章很感兴趣,所以今天继续给大家介绍文章相关的知识,本文《每个开发人员都应该掌握的 JavaScript 数组方法(第 1 部分)》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

“能力越大,责任越大。”
— 本叔叔,蜘蛛侠 (2002)
就像蜘蛛侠必须掌握他新发现的能力一样,开发人员需要掌握 javascript 强大的数组方法才能高效、负责任地进行编码。
让我们深入研究一些必须知道的数组方法!
1. 查找
find() 方法返回满足所提供的测试函数的第一个数组元素的值。
arr.find(callback(element, index, arr),thisarg)
- 返回数组中满足给定函数的第一个元素的值。
- 如果没有元素满足该函数,则返回未定义
const cuties = [
{ name: "wanda maximoff", age: 31 },
{ name: "natasha romanoff", age: 32 },
{ name: "jane foster", age: 27 },
{ name: "gwen stacy", age: 26 },
];
// find method returns the value of the first element
// in the array that satisfies the given function
// or else returns undefined
let cuty = cuties.find(({ age }) => age >= 30);
// output: { name: 'wanda maximoff', age: 31 }
console.log(cuty);
2. 查找索引
findindex() 方法返回满足所提供的测试函数的第一个数组元素的索引,否则返回 -1。
arr.findindex(callback(element, index, arr),thisarg)
- 返回数组中满足给定函数的第一个元素的索引。
- 如果没有元素满足该函数,则返回-1。
const cuties = [
{ name: "wanda maximoff", age: 31 },
{ name: "natasha romanoff", age: 32 },
{ name: "jane foster", age: 27 },
{ name: "gwen stacy", age: 26 },
];
// findindex method returns the index of the first array element
// that satisfies the provided test function or else returns -1.
let cutyindex = cuties.findindex(({ age }) => age >= 30);
// output: 0
console.log(cutyindex);
3.索引
indexof() 方法返回数组元素出现的第一个索引,如果未找到,则返回 -1。
arr.indexof(searchelement, fromindex)
- 如果元素在数组中至少出现一次,则返回该元素的第一个索引。
- 如果在数组中未找到该元素,则返回-1。
const productprices = [5, 12, 3, 20, 5, 2, 50]; // indexof() returns the first index of occurance // of an array element, or -1 if it is not found. let firstindex = productprices.indexof(20); console.log(firstindex); // 3 let secondindex = productprices.indexof(5); console.log(secondindex); // 0 // the second argument specifies the search start index let thirdindex = productprices.indexof(5, 1); console.log(thirdindex); // 4 // indexof returns -1 if not found let notfoundindex = productprices.indexof(15); console.log(notfoundindex); // -1
4. 排序
sort() 方法按特定顺序(升序或降序)对数组的项目进行排序。
arr.sort(comparefunction)
- 对数组元素进行排序后返回数组(这意味着它更改了原始数组并且不进行复制)。
const avengers = ["captain", "tony", "thor",
"natasha", "bruce", "clint"];
// modifies the array in place
avengers.sort();
// [ 'bruce', 'captain', 'clint', 'natasha', 'thor', 'tony' ]
console.log(avengers);
const nums = [1000, 50, 2, 7, 14];
// number is converted to string and sorted
nums.sort();
// output: [ 1000, 14, 2, 50, 7 ]
console.log(nums)
// sort nums in ascending order by providing compare function
nums.sort((a, b) => a - b);
// output: [ 2, 7, 14, 50, 1000 ]
console.log(nums);
5. 包括
includes() 方法检查数组是否包含指定元素。
arr.includes(valuetofind, fromindex)
includes() 方法返回:
- 如果在数组 searchvalue 中的任何位置找到,则为 true
- 如果在数组 searchvalue 中找不到任何位置,则返回 false
const avengers = ["captain", "tony", "thor",
"natasha", "bruce", "clint"];
// includes() method returns true if an array contains
// a specified element or else returns false.
let check1 = avengers.includes("thor");
console.log(check1); // true
// second argument specifies position to start the search
let check2 = avengers.includes("thor", 3);
console.log(check2); // false
// the search starts from the 4th-to-last element ("hulk")
// and checks the rest of the array for "thor".
let check3 = avengers.includes("thor", -4);
console.log(check3); // true
6. foreach
foreach() 方法为每个数组元素执行提供的函数。
arr.foreach(callback(currentvalue), thisarg)
• foreach() 不会对没有值的数组元素执行回调。
• 返回未定义。
const nums = [120, 150, 80, , 200];
// foreach() method executes a provided
// function for each array element which
// have values. it returns undefined.
/*
num 0: 120
num 1: 150
num 2: 80
num 4: 200
*/
nums.foreach((value, index) => {
console.log('num ' + index + ': ' + value);
});
7. 切片
slice() 方法将数组的一部分的浅拷贝返回到新的数组对象中。
arr.slice(开始, 结束)
• 返回包含提取元素的新数组。
const fruits = ["apple", "banana", "orange", "grape", "mango"]; // slicing the array (from start to end) let slicedfruits1 = fruits.slice(); console.log(slicedfruits1); // [ 'apple', 'banana', 'orange', 'grape', 'mango' ] // slicing from the third element let slicedfruits2 = fruits.slice(2); console.log(slicedfruits2); // [ 'orange', 'grape', 'mango' ] // slicing from the second element to fourth element let slicedfruits3 = fruits.slice(1, 4); console.log(slicedfruits3); // [ 'banana', 'orange', 'grape' ] // slicing the array from start to second-to-last let slicedfruits4 = fruits.slice(0, -1); console.log(slicedfruits4); // [ 'apple', 'banana', 'orange', 'grape' ] // slicing the array from third-to-last let slicedfruits5 = fruits.slice(-3); console.log(slicedfruits5); // [ 'orange', 'grape', 'mango' ]
8. 拼接
splice() 方法修改数组(添加、删除或替换元素)。
arr.splice(start, deletecount, item1, ..., itemn)
• 返回包含已删除元素的数组。
let animals = ["dog", "cat", "elephant", "lion"]; // replacing "elephant" & "lion" with "tiger" & "giraffe" let removedanimals1 = animals.splice(2, 2, "tiger", "giraffe"); console.log(removedanimals1); // [ 'elephant', 'lion' ] console.log(animals); // [ 'dog', 'cat', 'tiger', 'giraffe' ] // adding elements without deleting existing elements let removedanimals2 = animals.splice(1, 0, "elephant", "lion"); console.log(removedanimals2); // [] console.log(animals); // [ 'dog', 'elephant', 'lion', 'cat', 'tiger', 'giraffe' ] // removing 3 elements let removedanimals3 = animals.splice(2, 3); console.log(removedanimals3); // [ 'lion', 'cat', 'tiger' ] console.log(animals); // [ 'dog', 'elephant', 'giraffe' ]
9. 每个
every() 方法检查所有数组元素是否通过给定的测试函数。
arr.every(callback(currentvalue), thisarg)
every() 方法返回:
- true - 如果所有数组元素都通过给定的测试函数(回调返回真值)。
- false - 如果任何数组元素未通过给定的测试函数。
const nums1 = [ 1 , 2 , 3 , 4 , 5]; // every() method returns true if all the array // elements pass the given test function or else // returns false let result1 = nums1.every(element => element < 6); // output: true console.log(result1); const nums2 = [ 1 , 2 , 7 , 4 , 5]; let result2 = nums2.every(element => element < 6); // output: false console.log(result2);
10.一些
some() 方法测试是否有任何数组元素通过给定的测试函数。
arr.some(callback(currentvalue), thisarg)
- 如果数组元素通过给定的测试函数,则返回 true(回调返回真值)。
- 否则返回 false
const nums1 = [ 8 , 2 , 7 , 9 , 6]; // some() method returns true if any of the array // elements pass the given test function or else // returns false let result1 = nums1.some(element => element < 6); // Output: true console.log(result1); const nums2 = [ 8 , 6 , 7 , 9 , 10]; let result2 = nums2.some(element => element < 6); // Output: false console.log(result2);
请继续关注我们系列的第 2 部分,我们将深入探讨更重要的 javascript 数组方法!快乐学习!
本篇关于《每个开发人员都应该掌握的 JavaScript 数组方法(第 1 部分)》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!
PHP 函数中如何使用递归生成随机数?
- 上一篇
- PHP 函数中如何使用递归生成随机数?
- 下一篇
- Windows系统中通过route命令添加自定义永久路由的方法
-
- 文章 · 前端 | 3分钟前 |
- HTML5WebAudioAPI应用与音频处理教程
- 429浏览 收藏
-
- 文章 · 前端 | 4分钟前 |
- 项目1项目2项目3苹果香蕉橘子
- 151浏览 收藏
-
- 文章 · 前端 | 14分钟前 | CSSanimation transform-origin @keyframes transform:rotate() 旋转文字
- CSS实现旋转文字效果教程
- 343浏览 收藏
-
- 文章 · 前端 | 15分钟前 |
- 函数绑定与this控制技巧详解
- 152浏览 收藏
-
- 文章 · 前端 | 17分钟前 | CSS 头像 border-radius overflow:hidden 圆形裁剪
- CSS头像圆角裁剪方法解析
- 307浏览 收藏
-
- 文章 · 前端 | 19分钟前 |
- 清除浮动对CSS动画的影响分析
- 260浏览 收藏
-
- 文章 · 前端 | 24分钟前 |
- PHP动态显示数据库查询结果到Textarea的方法
- 150浏览 收藏
-
- 文章 · 前端 | 36分钟前 |
- 模板字符串进阶技巧与实战应用
- 341浏览 收藏
-
- 文章 · 前端 | 37分钟前 |
- 移动端JS触摸事件与手势识别教程
- 324浏览 收藏
-
- 文章 · 前端 | 39分钟前 |
- CSS表格宽度控制与布局优化技巧
- 262浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3202次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3415次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3445次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4553次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3823次使用
-
- JavaScript函数定义及示例详解
- 2025-05-11 502浏览
-
- 优化用户界面体验的秘密武器:CSS开发项目经验大揭秘
- 2023-11-03 501浏览
-
- 使用微信小程序实现图片轮播特效
- 2023-11-21 501浏览
-
- 解析sessionStorage的存储能力与限制
- 2024-01-11 501浏览
-
- 探索冒泡活动对于团队合作的推动力
- 2024-01-13 501浏览

