数组的高级用法

数组的高级用法

      forEach((item, index, arr) => {})
      map
      filter
      some
      every
      reduce
      find
      findIndex

经常使用的有
map filter some find

例子

const stus = [
  {
    name: "张三",
    age: 19,
    sex: '男'
  },{
    name: "李四",
    age: 20,
    sex: '女'
  },{
    name: "王五",
    age: 18,
    sex: '女'
  },{
    name: "赵六",
    age: 16,
    sex: '男'
  }
]

把这些学生放在东湖

    stus.forEach(student => student.classroom = "东湖")
    console.log(stus)

把所有的学生姓名统计一下 会把返回值放在新数组

const names = stus.map(student => student.name)
console.log(names)

//获得所有的男生 如果返回值为true,则把当时正在遍历的student放在新数组

    const male = stus.filter(student => student.sex === '男')
    console.log(male)

// 有没有大于18岁的

    const isGte18 = stus.some(student => student.age >= 18)
    console.log(isGte18)

// 是不是都大于等于18岁

const isAllGte18 = stus.every(student => student.age >= 18)
console.log(isAllGte18)

// 年龄之和

    const ageTotal = stus.reduce((sum, student) => sum += student.age, 0)
    console.log(ageTotal)

// 查找王五

    const _student = stus.find(student => student.name === "王五")
    console.log(_student)

// 查找下标

    const index = stus.findIndex(student => student.name === "王五")
    console.log(index)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值