Array的遍历

const aa=["1", "0", "0", "1"]

aa.find(i=>{ //find: 返回满足条件的数组的第一个元素的值
    return i=='1'
})
"1"
aa.map(i=>{  //map: 返回一个处理后的新数组
    return i+'a'
})
["1a", "0a", "0a", "1a"]
aa.some(i=>{  //some: 判断数组中是否有满足条件的元素
    return i=='1'
})
true
aa.includes('1')  //includes: 判断数组中是否包含指定的值
true
aa.forEach(i=>{  //forEach: 处理数组中的每个元素,不返回值
    const b = i+'b'
    console.log(b)
})
1b
0b
0b
1b
for(let i=0; i<aa.length; i++){  //for:遍历数组,不返回值,可根据条件跳出循环
    console.log(aa[i])
}
1
0
0
1
aa.filter(i=>{   //filter:过滤出数组中所有符号条件的元素,返回一个新数组
    return i > 0
})
["1", "1"]

arr.map() 是为了返回值的

arr.forEach() 是为了处理但不返回值的

arr.filter() 是过滤值的

如果要跳出循环,还是用for

原文地址:https://www.cnblogs.com/qq254980080/p/13222948.html