JavaScript遍历循环

Stella981
• 阅读 538

定义一个数组和对象

const arr = ['a', 'b', 'c', 'd', 'e', 'f'];
const obj = {
    a: 1,
    b: 2,
    c: 3,
    d: 4
}

for()

经常用来遍历数组元素
遍历值为数组元素索引

for (let i = 0, len = arr.length; i < len; i++) {
    console.log(i);            // 0 1 2 3 4 5
    console.log(arr[i]);     // a b c d e f
}

forEach()

用来遍历数组元素
第一个参数为数组元素,第二个参数为数组元素索引,第三个参数为数组本身(可选)
没有返回值

arr.forEach((item, index) => {
    console.log(item);     // a b c d e f 
    console.log(index);   // 0 1 2 3 4 5
})

map()

用来遍历数组元素
第一个参数为数组元素,第二个参数为数组元素索引,第三个参数为数组本身(可选)
有返回值,返回一个新数组

every(),some(),filter(),reduce(),reduceRight()不再一一介绍

let arrData = arr.map((item, index) => {
    console.log(item);     // a b c d e f 
    console.log(index);   // 0 1 2 3 4 5
    return item;
})
console.log(arrData);    // ["a", "b", "c", "d", "e", "f"]

for...in

可循环对象和数组,推荐用于循环对象

1.循环值为对象属性

for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
        console.log(key);           // a b c d  属性
        console.log(obj[key]);    // 1 2 3 4  属性值
    }
}

2.值为数组索引

for (let index in arr) {
    console.log(index);          // 0 1 2 3 4 5 数组索引
    console.log(arr[index]);   // a b c d e f 数组值
}

当我们给数组添加一个属性name

arr.name = '我是自定义的属性'

for (let index in arr) {
    console.log(index);           // 0 1 2 3 4 5 name (会遍历出我们自定义的属性)
    console.log(arr[index]);    // a b c d e f 我是自定义属性name
}

for...of

可循环对象和数组,推荐用于遍历数组

1.遍历值为数组元素

for (let value of arr) {
    console.log(value);       // a b c d e f 数组值
}

2.遍历对象时须配合Object.keys()一起使用,直接用于循环对象会报错,不推荐使用for...of循环对象
循环值为对象属性

for (let value of Object.keys(obj)) {
    console.log(value);    // a b c d 对象属性
}

总结

  • 用于遍历数组元素使用:for(),forEach(),map(),for...of
  • 用于循环对象属性使用:for...in
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Wesley13 Wesley13
2年前
Java开发者容易犯的十个错误
!(https://oscimg.oschina.net/oscnet/c9f00cc918684fbe8a865119d104090b.gif)Top1.数组转换为数组列表将数组转换为数组列表,开发者经常会这样做:\java\List<StringlistArrays.asList(arr);Arr
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
JavaScript中遍历数组和对象的方法
js数组遍历和对象遍历针对js各种遍历作一个总结分析,从类型用处,分析数组和对象各种遍历使用场景,优缺点等JS数组遍历:1,普通for循环,经常用的数组遍历vararr1,2,0,3,9,10,20,
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Stella981 Stella981
2年前
HashMap 的底层实现原理
HashMap是一个用于存储KeyValue键值对的集合,每一个键值对也叫做Entry。这些个Entry分散存储在一个数组当中,这个数组就是HashMap的主干。HashMap数组每一个元素的初始值都是Null。 !(https://oscimg.oschina.net/oscnet/8495d30fe00a2865dd74088d2
Stella981 Stella981
2年前
ES6 for in与for of 的使用方法及其区别
  // for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值。 let arr  \1,2,3,4,5,6,7\    for(let index of arr){     //   console.log(index)//1 2 3 4 5 6 7    }    for(let index in
Wesley13 Wesley13
2年前
ES6 新增的数组的方法
给定一个数组letlist\//wu:武力zhi:智力{id:1,name:'张飞',wu:97,zhi:10},{id:2,name:'诸葛亮',wu:55,zhi:99},{id:3,name:'赵云',wu:97,zhi:66},{id:4,na
Wesley13 Wesley13
2年前
5种方式实现数组扁平化
数组扁平化概念数组扁平化是指将一个多维数组变为一维数组1,2,3,4,51,2,3,4,5实现1\.reduce遍历数组每一项,若值为数组则递归遍历,否则concat。functionflatten(arr){