微信小程序 JS 判断两个对象中指定数据是否相等,并以对象的形式存储到一个数组中(数组对象)

公共属性
 1 let oldData = [
 2 { id: 1, price: 100, skuid: 233 }, 
 3 { id: 2, price: 200, skuid: 2311 }, 
 4 { id: 3, price: 300, skuid: 312 }, 
 5 { id: 4, price: 180, skuid: 23342 }
 6 ]
 7 
 8 let newData = [
 9 { price: 136, skuid: 312 }, 
10 { price: 195, skuid: 233 }, 
11 { price: 155, skuid: 2311 }
12 ]
13 
14 let priceChange = []

 

 一  双for循环判断

 1  for (let i = 0; i < newData.length; i++) {
 2      for (let j = 0; j < oldData.length; j++) {
 3          if(newData[i].skuid == oldData[j].skuid){
 4             priceChange.push({
 5                 'id':oldData[j].id,
 6                 'price':newData[i].price,
 7                 'skuid':newData[i].skuid
 8             })
 9          } 
10      }
11  }
12  console.log(priceChange);

 输出

 

二  obj.filter(),返回一符合要求的数组
 1 for (let i in oldData) {
 2     let old_skuid = oldData[i].skuid
 3     if (i != oldData.length - 1) {
 4         let res = newData.filter(item => {
 5             return item.skuid == old_skuid
 6         })
 7         priceChange.push({
 8             'skuid':res[0].skuid,
 9             'price':res[0].price
10         })
11     }
12 }
13 console.log('priceChange',priceChange);

 输出

 

posted @ 2021-01-26 15:03  伊凡晴天  阅读(3167)  评论(0编辑  收藏  举报