JS数组遍历的几种方法

  1. for
      最简单的一种循环遍历方法,也是使用频率最高的一种,可优化
var arr = [1, 2, 3, 4, 5, 6]
for(var i = 0; i < arr.length; i++) {
    console.log(arr[i])
}
// 1 2 3 4 5 6

优化:使用临时变量,将长度缓存起来,避免重复获取数组长度,当数组较大时优化效果才会比较明显

var arr = [1, 2, 3, 4, 5, 6]
var len = arr.length
for(var i = 0; i < len; i++) {
    console.log(arr[i])
}
// 1 2 3 4 5 6
  1. for…in…
      效率最低(输出的 key 是数组索引)
var arr = ['苹果', '橘子', '香蕉', '葡萄', '草莓', '芒果']
for(var key in arr) {
    console.log(key)
}

// 0 1 2 3 4 5
  1. for…of…(ES6)
      性能要好于 for…in…,但仍然比不上普通的 for 循环(不能循环对象)
var arr = ['苹果', '橘子', '香蕉', '葡萄', '草莓', '芒果']
for(var key of arr) {
    console.log(key)
}
// 苹果 橘子 香蕉 葡萄 草莓 芒果
  1. forEach(ES6)
        数组里的元素个数有几个,该方法里的回调就会执行几次
        第一个参数是数组里的元素,第二个参数为数组里元素的索引,第三个参数则是它自己
        数组自带的遍历方法,虽然使用频率略高,但是性能仍然比普通循环略低
var arr = [1, 2, 3, 4, 5, 6]
arr.forEach(function (item, idnex, array) {
    console.log(item)     // 1 2 3 4 5 6
    console.log(array)    // [1, 2, 3, 4, 5, 6]
})
  1. map(ES6)
        遍历每一个元素并且返回对应的元素(可以返回处理后的元素) (map 映射 一 一 对应)
        返回的新数组和旧数组的长度是一样的
        使用比较广泛,但其性能还不如 forEach
var arr = [1, 2, 3, 4, 5, 6]
var newArr = arr.map(function (item, idnex) {
    // 第一个参数是值 第二个参数是索引值
    return item * item
})

console.log(newArr)    // [1, 4, 9, 16, 25, 36]
// 要添加代码块 map 不可以解析同一个块级作用域
// {}{}代表不同的块级作用域 分别在不同里面写

// 浅拷贝
// 浅拷贝是指a把值 给了b 当b的值改变 a b 的值同时改变。

let list = [
    { id: 23, title: "女装1",price: 100},
    { id: 23, title: "女装2",price: 200},
    { id: 23, title: "女装3",price: 300}
]
let newArr = data.list.map((item, index) => {
     item.price = item.price * .6
     return item;
 });
console.log(newArr)//打印的结果价格都是改变的,一样的
// {
    // 0: {id: 23, title: "女装1", price: 60}
    // 1: {id: 24, title: "女装2", price: 120}
    // 2: {id: 27, title: "女装3", price: 180}
 // }
 console.log(data.list)//同上

// 深拷贝
// 深拷贝是指a b 的值无论哪个值改变 内存地址不同 互不干扰
// 深拷贝1

let newArr = list.map(item => {
  // es6箭头函数,可以只写一个参数
  // 使用深拷贝需要重新定义一个空的来接收改变的数据
  let newPrice = item.price * 0.6
  return {
    id: item.id,
    title: item.title,
    price: newPrice
  }
})
console.log(newArr) // price对应的数据发生改变,同上
console.log(list)  // 原数据不发生改变

// 深拷贝 2(简单粗暴)

// JSON.stringify 把对象转为字符串
// JSON.parse 把字符串转化为对象
let newList = JSON.parse(JSON.stringify(list))
let newArr = newList.map(item => {
  item.price *= 0.6
  return item
})
console.log (newArr)  //同深拷贝1
console.log (list)  //同深拷贝1
  1. filter(ES6)
      遍历数组,过滤出符合条件的元素并返回一个新数组
var arr = [
    { id: 1, name: '小明', done: true },
    { id: 2, name: '小华', done: true },
    { id: 3, name: '小红', done: false }
]
    
var newArr = arr.filter(function (item, index) {
    return item.done
})

console.log(newArr)

// [{ id: 1, name: '小明', done: true },{ id: 2, name: '小华', done: true }]
  1. some(ES6)
      遍历数组,只要有一个以上的元素满足条件就返回 true,否则返回 false
var arr = [
    { id: 1, name: '小明', done: true },
    { id: 2, name: '小华', done: true },
    { id: 3, name: '小红', done: false }
]

var bool = arr.some(function (item, index) {
    return item.done
})

console.log(bool)    // true
  1. every(ES6)
      遍历数组,每一个元素都满足条件 则返回 true,否则返回 false
var arr = [
    { id: 1, name: '小明', done: true },
    { id: 2, name: '小华', done: true },
    { id: 3, name: '小红', done: false }
]

var bool = arr.every(function (item, index) {
    return item.done
})

console.log(bool)    // false
  1. find(ES6)
      遍历数组,返回符合条件的第一个元素,如果没有符合条件的元素则返回 undefined
var arr = [1, 1, 2, 2, 3, 3, 4, 5, 6]
    
var num = arr.find(function (item, index) {
    return item === 3
})

console.log(num)   //  3
  1. findIndex(ES6)
       遍历数组,返回符合条件的第一个元素的索引,如果没有符合条件的元素则返回 -1
var arr = [1, 1, 2, 2, 3, 3, 4, 5, 6]
    
var num = arr.findIndex(function (item) {
    return item === 3
})

console.log(num)   //  4
  1. reduce (ES6)
    用来实现累加的效果
    输出的总和是 sum+val(数值)
// reduce 用来实现累加的效果 (常用于写购物车价格的累加)
// 声明一个数组 数组里面放数字 让其里面的数字显示为累加的总和
let arr=[200,200,100]
let result =arr.reduce((sum,val,index)=>{
  200+200 index 
  400+100 index
// sum是总加后的和 val是变量里面的值 index为索引值 
  console.log(sum,val,index)  
  // 200 200 1
  // 400 100 2
  return sum +val;
})
console.log(result)  // 500
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容