javascript lodash/下划线;比较两个对象并删除重复项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28858853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
lodash/underscore; compare two objects and remove duplicates
提问by numediaweb
As you can see in the image below, I have some returned json data
with three objects; each contains a clients id => data.
如下图所示,我返回data
了一些包含三个对象的json ;每个都包含一个客户 ID => 数据。
exact_match : {104}
match_4 : {104, 103}
match_2 : {104, 103, 68}
How can I "trim" or remove the duplicate objects based on previous ones? something like:
如何根据以前的对象“修剪”或删除重复的对象?就像是:
exact_match : {104}
match_4 : {103}
match_2 : {68}
I tried _.differencebut did not work (Maybe because it is for arrays not objects?):
我试过_.difference但没有用(也许是因为它是用于数组而不是对象?):
var exact_match = data.exact_match,
match_four_digits = _.difference(data.match_4, data.exact_match),
match_two_digits = _.difference(data.match_2, data.exact_match, data.match_4),
Any help would be appreciated :)
任何帮助,将不胜感激 :)
Update
更新
I need that the returned value has the same object data instead of a new array :)
我需要返回的值具有相同的对象数据而不是新数组:)
采纳答案by numediaweb
Thanks guys for the answers, I really appreciate your time.
谢谢大家的回答,我真的很感谢你的时间。
I searched further and found this postby Lodash developer that helped me came up with this snippet;
我进一步搜索,发现Lodash 开发人员的这篇文章帮助我想出了这个片段;
var data = {
exact_match: {
104: {
supplier_id: 104
}
},
match_four_digits: {
104: {
supplier_id: 104
},
68: {
supplier_id: 68
}
},
match_two_digits: {
104: {
supplier_id: 104
},
68: {
supplier_id: 68
},
103: {
supplier_id: 103
},
999: {
supplier_id: 999
}
}
};
var arr_match_four_digits = _.difference(_.keys(data.match_four_digits), _.keys(data.exact_match));
var arr_match_two_digits = _.difference(_.keys(data.match_two_digits), _.keys(data.match_four_digits), _.keys(data.exact_match));
$('#output1').html(JSON.stringify(data));
$('#output2').html(JSON.stringify(_.pick(data.match_four_digits, arr_match_four_digits)));
$('#output3').html(JSON.stringify(_.pick(data.match_two_digits, arr_match_two_digits)));
<script src="https://cdn.rawgit.com/lodash/lodash/3.3.1/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
data
<pre><code><div id="output1"></div></code></pre>
arr_match_four_digits
<pre><code><div id="output2"></div></code></pre>
match_two_digits
<pre><code><div id="output3"></div></code></pre>
回答by Jonathan Allard
It looks like you want to diff keys (or rather, it'd be efficient to — _.keys
)
看起来你想要区分键(或者更确切地说,它会很有效 - _.keys
)
_.difference(
_.keys({104: 1, 102: 3, 101: 0}), // ["104", "102", "101"]
_.keys({104: 1, 102: 3}) // ["104", "102"]
)
// [ "101" ]
Or, you could always convert your object to an array of pairs if you want to compare within the objects too (_.pairs
):
或者,如果您也想在对象内进行比较,则始终可以将对象转换为成对数组 ( _.pairs
):
_.difference(
_.pairs({104: 1, 102: 3, 101: 0}), // [["104",1], ["102",3], ["101",0]]
_.pairs({104: 1, 102: 2}) // [["104",1], ["102",2]]
)
// [["102", 3], ["101", 0]]
回答by KhalilRavanna
I would create a map called unique
, e.g. var unique = {};
and then iterate over each key in your data and check if it's in unique
. If it is in unique
, delete the entry associated with that key, thus removing duplicates.
我会创建一个名为unique
, eg的地图,var unique = {};
然后遍历数据中的每个键并检查它是否在unique
. 如果它在 中unique
,则删除与该键关联的条目,从而删除重复项。
You could pull this check out as an alreadyFound
method:
你可以把这个检查作为一种alreadyFound
方法:
var alreadyFound = function (key) {
if (!(key in unique)) {
unique[key] = true;
return false;
}
return true;
};
Then iterate over your data and check alreadyFound(key)
for key
in your data, and delete the key if alreadyFound(key)
returns true
.
然后循环在你的数据,并检查alreadyFound(key)
了key
您的数据,并删除如果键alreadyFound(key)
的回报true
。
You could go messing with lodash/underscore methods but those might be inefficient depending on how you use them (and how they're implemented) and this should operate in linear time.
您可以使用 lodash/underscore 方法,但这些方法可能效率低下,具体取决于您如何使用它们(以及它们的实现方式),并且这应该在线性时间内运行。
It looks like for your specific use case the full solution would be something like:
对于您的特定用例,完整的解决方案看起来像:
var unique = {};
// Assume I copy and pasted alreadyFound here
var alreadyFound = ...;
for (var object in data) {
// Iterate through ids in each object in data
for (var id in object) {
// Remove this entry if it's already found
if (alreadyFound(id)) {
delete object[id];
}
}
}