map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
let arr = [66, 88, 33, 55, 68]
var newArr = arr.map((value)=>{
console.log(value, '')
return value *2
})
console.log(newArr)
some() 查找是否有满足条件的元素(查找到第一个则终止循环)
arr.some((value) => {
console.log(value)
if (value === 66) {
// some里面可以使用return,减少没必要的循环
return true
}
})
forEach()
let arr = [66, 88, 33, 55, 68]
arr.forEach((value, index, arr) => {
console.log('每一个值', value)
console.log('索引', index)
console.log('arr', arr)
//forEach里面的return是不生效的
return false
})
filter()过滤器,返回一个新的数组
//
var newArr = arr.filter((value,index,arr) => {
console.log(value,index,arr)
return value >55
})
console.log(newArr)
find(返回一个函数,输出满足条件的第一个值,找到后退出循环)
let arr = [
{
id: 1,
name: '张珊'
}, {
id: 2,
name: '李⑩'
}
]
let findArr = arr.find((item,index)=>{
return item.id==1
})
console.log(findArr) //{id: 1, name: "张珊"}
findIndex用于查找符合条件成员的位置,没有找到返回-1
let arr = [1, 3, 5, 7, 9]
var index = arr.findIndex((item, index) => item > 7)
console.log(index) //4