- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个对象数组,如:[{id: '123', name: 'John', someKey:'1234'}, {id: '123', name: 'John', someKey:'12345'}]
这只是一个基本的例子,数据要复杂得多所以_.isEqual
不起作用。
我该如何处理比较器?我想比较id
如果他们相等。_.uniqWith(myArray, function(something) {return something})
最佳答案
比较 _.uniqWith()
中的 ID比较器函数或使用 _.uniqBy()
:
var myArray = [{
id: '123',
name: 'John',
someKey: '1234'
}, {
id: '123',
name: 'John',
someKey: '12345'
}]
var result = _.uniqWith(myArray, function(arrVal, othVal) {
return arrVal.id === othVal.id;
});
console.log(result);
/** using uniqBy **/
var result = _.uniqBy(myArray, 'id');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>
关于lodash uniqWith 与一组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381771/
我有一个对象数组,如: [{id: '123', name: 'John', someKey:'1234'}, {id: '123', name: 'John', someKey:'12345'}]
_.uniqWith(newSelectedModelList, _.isEqual); 对我的性能有很大的影响。 我正在比较一个包含 2100 个元素的数组。每个元素都有 { name: 'foo'
我在 Lodash 中使用 _.uniqWith 从约 6,000 个对象的数组中删除重复对象: let uniqColors = _.uniqWith(colorCollection, _.isEq
您好,我正在使用 lodash uniqWith 方法删除数组中具有相同 ID 的重复项。 但是 lodash 保留第一个重复项。但我不想保留最后一个重复的项目。 我能为此做什么? var resul
我是一名优秀的程序员,十分优秀!