删除数组对象中重复的对象

44次阅读

共计 346 个字符,预计需要花费 1 分钟才能阅读完成。

删除数组对象中重复的对象

var goodsList = [{goods:{id:1},price:1,count:1},
      {goods:{id:1},price:1,count:1},
      {goods:{id:2},price:2,count:2},
      {goods:{id:2},price:2,count:2}
];
for(var i=0; i<goodsList.length; i++){var item = goodsList[i]
      for(var j = i+1; j<goodsList.length; j++){if(item.goods.id == goodsList[j].goods.id){goodsList.splice(j,1)
              i--
            }                          
      }
};
console.log(goodsList);

打印的结果如下图所示:

正文完
 0

删除数组对象中重复的对象

44次阅读

共计 346 个字符,预计需要花费 1 分钟才能阅读完成。

删除数组对象中重复的对象

var goodsList = [{goods:{id:1},price:1,count:1},
      {goods:{id:1},price:1,count:1},
      {goods:{id:2},price:2,count:2},
      {goods:{id:2},price:2,count:2}
];
for(var i=0; i<goodsList.length; i++){var item = goodsList[i]
      for(var j = i+1; j<goodsList.length; j++){if(item.goods.id == goodsList[j].goods.id){goodsList.splice(j,1)
              i--
            }                          
      }
};
console.log(goodsList);

打印的结果如下图所示:

正文完
 0