數組比較的n+

模擬數據app

data () {
    return {
      array: [
        {
          id: 1,
          name: 雲朵,
          statevoid: false
        },
        {
          id: 2,
          name: 白雲,
          statevoid: false
        },
        {
          id: 3,
          name: 彩虹,
          statevoid: false
        }
     ],
     parray: [
       {
         id: 2,
         name: 雲朵,
         statevoid: false
      },
      {
        id: 6,
        name: 白雲,
        statevoid: false
      },
      {
        id: 7,
        name: 彩虹,
        statevoid: false
      }
    ]
  }
}

js部分this

var mapper = {}
this.array.forEach(function (item, index, array) {
  mapper[item.id] = index
})
// console.log(mapper)
let that = this
this.parray.forEach(function (item, index, array) {
  let value = mapper[item.id]
  // console.log(value)
  if (value !== undefined) {
    that.array[value].statevoid = true
  }
})
console.log(this.array)
相關文章
相關標籤/搜索