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'
              }
            })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM