我有一系列的对象,如:
[{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);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7063 次 |
| 最近记录: |