gpt4 book ai didi

lodash uniqWith 与一组对象

转载 作者:行者123 更新时间:2023-12-03 22:54:01 31 4
gpt4 key购买 nike

我有一个对象数组,如:
[{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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com