• find 和 map的应用


    find

    find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

    find() 方法为数组中的每个元素都调用一次函数执行:

    • 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
    • 如果没有符合条件的元素返回 undefined

    注意: find() 对于空数组,函数是不会执行的。

    注意: find() 并没有改变数组的原始值。

    1.找到对应的值

        
    let week = 2
    let weekName
    = this.getWeekName(week) weeksOptions: [ {value: '1', name: '周一'}, {value: '2', name: '周二'}, {value: '3', name: '周三'}, {value: '4', name: '周四'}, {value: '5', name: '周五'}, {value: '6', name: '周六'}, {value: '7', name: '周日'} ], getWeekName (week) { return this.weeksOptions.find(item => { return item.value === week + '' }).name },

     2.筛选出符合条件的,过滤掉

              let thisTime = this.addTimeForm.timeList.find(item => {
                return ((this.beginTime <= item.bt && this.endTime >= item.bt) || (this.endTime >= item.et && this.beginTime <= item.et) || (this.endTime <= item.et && this.beginTime >= item.bt))
              })
              console.log('thisTime:', thisTime)
              if (thisTime) {
                this.sentMsg('所选时间段已存在')
                return
              } else {
                console.log('thisTime:', thisTime)
              }

    map

    map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

    map() 方法按照原始数组元素顺序依次处理元素。

    注意: map() 不会对空数组进行检测。

    注意: map() 不会改变原始数组。

               this.table1.array = res.body.data.advTimeList.map((item, index) => {
                  item.weekName = this.getWeekName(item.week)
                  item.operation = {
                    id: index + 'operation'
                  }
                })
  • 相关阅读:
    iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)
    iOS中 学会如何对sqlite3 进行封装
    杭电ACM2012素数判定
    杭电ACM2503a/b+c/d
    杭电ACM2005第几天
    杭电ACM2034人见人爱AB
    杭电ACM2502月之数
    杭电ACM2001计算两点间的距离
    杭电ACM2002计算求得体积
    杭电ACM2033人见人爱A+B
  • 原文地址:https://www.cnblogs.com/benbendu/p/12844967.html
Copyright © 2020-2023  润新知