ES6 檢測數組中是否存在滿足某些條件的元素實現方式


let arr = [2, 5, 8, 1, 4]

// filter 實現 返回 數組
let result = arr.filter(item => {
    return item > 10
}).length > 0

// find 實現  返回所匹配的那一項
let result = arr.find(item => {
    return item > 10
})  !== undefined

// some 實現  返回true  false
let result =  arr.some(item => {
    return item > 10
})
addChannelCart: function () {
    const that = this;
    // console.log(that.data.warehouse_id, '添加購物車==>warehouse_id')
    let result = that.data.cart_list.some(item => {
      return item.warehouse_id == that.data.warehouse_id })
    console.log(result, 'result')
    if (!result) {
      wx.showToast({
        title: '',
        icon: 'none'
      })
    }
    return;
    if (!wx.getStorageSync('brand_shop_id')) {
      wx.showToast({
        title: '添加購物車無brand_shop_id',
      })
    }

 


免責聲明!

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



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