博客
关于我
js数组遍历十种方法
阅读量:232 次
发布时间:2019-03-01

本文共 3036 字,大约阅读时间需要 10 分钟。

JavaScript 数组方法实用解析

1. some()

some() 方法用于检查数组中是否存在至少一个满足条件的元素。如果找到符合条件的元素,立即返回 true,否则返回 false

private some(id: number) {    const arr = [        { cityId: 195, cityName: '深圳' },        { cityId: 196, cityName: '北京' },        { cityId: 198, cityName: '上海' }    ];    const result = arr.some((item: any) => item.cityId === id);    console.log(`传入:${id}, 结果:${result}`);}

2. every()

every() 方法用于检查数组中的每一个元素是否都满足给定的条件。如果所有元素都满足条件,则返回 true,否则返回 false

private every() {    const arr = [1, 2, 3, 4, 5];    const result = arr.every((item: any) => item > 0);    console.log(`结果:${result}`);}

3. forEach()

forEach() 方法用于对数组中的每个元素执行回调函数。该方法不会改变原数组,并且不支持 continuebreak,建议使用 returnthrow 来控制循环。

private forEach() {    type itemType = { cityId: number, cityName: string };    const arr = [        { cityId: 195, cityName: '深圳' },        { cityId: 196, cityName: '北京' },        { cityId: 197, cityName: '上海' }    ];    arr.forEach((item: itemType, index: number, arr: any) => {        console.log(`index: ${index}, item: ${JSON.stringify(item)}, arr: ${JSON.stringify(arr)}`);    });}

4. map()

map() 方法用于创建一个新数组,新数组中的每个元素是原数组对应元素调用函数处理后的结果。该方法不会改变原数组。

let arr = [1, 2, 3, 4, 5, 6];let newArr = arr.map((item: any) => item * item);console.log(newArr);

5. filter()

filter() 方法用于创建一个新数组,包含原数组中符合条件的所有元素。该方法不会改变原数组。

private filter(id: number): string {    const arr = [        { cityId: 195, cityName: '深圳' },        { cityId: 196, cityName: '北京' },        { cityId: 197, cityName: '上海' }    ];    let name: string = '';    const result = arr.filter((item: any) => {        if (item.cityId === id) {            name = item.cityName;        }    });    console.log(`传入:${id},结果:${name}`);    return name;}

6. find()

find() 方法用于遍历数组,返回符合条件的第一个元素。如果没有找到符合条件的元素,则返回 undefined

let arr = [1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6];let num = arr.find((item: any) => item === 3);console.log(num);

7. findIndex()

findIndex() 方法与 find() 类似,但返回的是符合条件的第一个元素的索引。如果没有找到符合条件的元素,则返回 -1

let arr = [1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6];let num = arr.findIndex((item: any) => item === 2);console.log(num);

8. for...of (ES6)

for...of 方法用于遍历数组,自动解构当前元素和索引,适合处理数组的迭代操作。

const arr = [    { cityId: 195, cityName: '深圳' },    { cityId: 196, cityName: '北京' },    { cityId: 197, cityName: '上海' }];for (const { cityId, cityName } of arr) {    console.log(cityId, cityName);}

9. for...in

for...in 方法用于遍历数组或对象的属性。对于数组,for...in 会遍历元素的索引字符串。

const arr = [    { cityId: 195, cityName: '深圳' },    { cityId: 196, cityName: '北京' },    { cityId: 197, cityName: '上海' }];const obj = { cityId: 195, cityName: '深圳' };for (const key in arr) {    console.log(`数组key-${key}`);}for (const key in obj) {    console.log(`对象key-${key}`);}

10. for 循环

for 循环是最基础的循环语句,适合对数组进行索引遍历。虽然可读性较低,但性能优异。

const arr = [    { cityId: 195, cityName: '深圳' },    { cityId: 196, cityName: '北京' },    { cityId: 197, cityName: '上海' }];for (let i = 0; i < arr.length; i++) {    console.log(arr[i]);}

性能对比

  • for:速度最快,但可读性较差。
  • forEach:性能较好,适合控制内容。
  • for...of:速度较慢,但支持自动解构。
  • for...in:适合处理对象属性,但数组性能较差。

通过合理选择这些方法,可以高效地完成数组操作,提升代码可读性和性能。

转载地址:http://zdgv.baihongyu.com/

你可能感兴趣的文章
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP三大特征抽取器:CNN、RNN与Transformer全面解析
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP度量指标BELU真的完美么?
查看>>
NLP的不同研究领域和最新发展的概述
查看>>
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>