Lodash从数组中删除重复项
这是我的数据:
[ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: "23" }, { url: 'www.example.com/i-like-cats', id: "24" }, { url: 'www.example.com/i-like-pie', id: "25" } ] 用Lodash,我怎么能删除具有重复的ID键的对象? filter,地图和独特的东西,但不是很确定。
我的实际数据集要大得多,而且有更多的关键字,但概念应该是一样的。
  _.uniq不再适用于当前版本,因为lodash 4.0.0有这个突破性的改变 。 所以使用任一 
 _.uniqBy(data, function (e) { return e.id; }); 
要么
 _.uniqBy(data, 'id'); 
文档: https : //lodash.com/docs#uniqBy
对于老版本的lodash(<4.0.0)
 假设数据应该是唯一的id和您的数据存储在datavariables中,您可以使用uniq()函数,如下所示: 
 _.uniq(data, function (e) { return e.id; }); 
或干脆
 _.uniq(data, 'id'); 
你可以使用lodash方法_.uniqWith,它可以在当前版本的lodash 4.17.2中使用。
例:
 var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; _.uniqWith(objects, _.isEqual); // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }] 
更多信息: https : //lodash.com/docs/#uniqWith