【发布时间】:2018-06-27 18:18:24
【问题描述】:
我需要你的帮助...如果问题已经被问过,但我似乎无法找到适合我的问题的答案,我很抱歉:我正在尝试提取(而不是删除) 我的数组中的重复项列表。
最终,主要目标是只保留一个重复的对象(在数组中)具有更高的利润......
这是我的数组的一个简单示例:
var arr = [
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 10 },
availability: true,
option: 1
},
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 15 },
availability: false,
option: 2
},
{
mkBase: "test1",
mkComp: "test",
invest: { profit: 8 },
availability: true,
option: 3
},
{
mkBase: "test2",
mkComp: "test",
invest: { profit: 6 },
availability: true,
option: 4
},
{
mkBase: "test",
mkComp: "test2",
invest: { profit: 6 },
availability: true,
option: 5
},
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 7 },
availability: true,
option: 6
},
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 10 },
availability: true,
option: 7
},
{
mkBase: "test3",
mkComp: "test4",
invest: { profit: 10 },
availability: true,
option: 8
}
];
我设法提取了几乎所有重复项的列表:
for (var i = 0; i < arr.length; i++) {
if (_.uniqBy(arr, "mkBase").indexOf(arr[i]) == -1) {
console.log("[SAME BASE]: " + JSON.stringify(arr[i], null, 2));
} else if (_.uniqBy(arr, "mkComp").indexOf(arr[i]) == -1) {
console.log("[SAME COMP]: " + JSON.stringify(arr[i], null, 2));
}
}
结果如下:
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test1",
"invest": {
"profit": 15
},
"availability": false,
"option": 2
}
[SAME COMP]: {
"mkBase": "test2",
"mkComp": "test",
"invest": {
"profit": 6
},
"availability": true,
"option": 4
}
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test2",
"invest": {
"profit": 6
},
"availability": true,
"option": 5
}
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test3",
"invest": {
"profit": 7
},
"availability": true,
"option": 6
}
[SAME BASE]: {
"mkBase": "test",
"mkComp": "test3",
"invest": {
"profit": 10
},
"availability": true,
"option": 7
}
Lodash 方法 (_.uniqBy) 将其中一个副本保留在主数组中,并且,为了最终获得最好的副本 (_.maxBy(arr, 'profit')),我会需要它与其他副本。
我不确定我是否很清楚,但如果您需要任何澄清,请告诉我!
提前感谢大家!
********** 编辑 ************* 正如 stasovlas 所建议的,您会在预期结果下方找到数组中其他对象被删除的原因:
var result = [
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 15 },
availability: false,
option: 2
},
{
mkBase: "test1",
mkComp: "test",
invest: { profit: 8 },
availability: true,
option: 3
},
{
mkBase: "test3",
mkComp: "test4",
invest: { profit: 10 },
availability: true,
option: 8
}
];
var removed = [
//Reason: Same Base **and** Comp mk as option 2 && Profit is too low versus option 2
{
mkBase: "test",
mkComp: "test1",
invest: { profit: 10 },
availability: true,
option: 1
},
//Reason: Same Comp mk as option 3 && Profit is too low versus option 3
{
mkBase: "test2",
mkComp: "test",
invest: { profit: 6 },
availability: true,
option: 4
//Reason: Same Base mk as option 2 && Profit is too low versus option 2
},
{
mkBase: "test",
mkComp: "test2",
invest: { profit: 6 },
availability: true,
option: 5
},
//Reason: Same Base mk as option 2 && Profit is too low versus option 2
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 7 },
availability: true,
option: 6
},
//Reason: Same Base mk as option 2 && Profit is too low versus option 2
{
mkBase: "test",
mkComp: "test3",
invest: { profit: 10 },
availability: true,
option: 7
}
];
【问题讨论】:
-
您的应用中是否需要随时重复的项目?你可能想看看这个关于数据结构的线程:“当你真正需要一个集合时不要使用数组”:stackoverflow.com/questions/40055764/…我知道上下文是 Firebase,但你可能会在那里找到答案
-
谢谢,我会调查它,但我对 Firebase 一点也不熟悉。为了回答你的问题,不,我以后不需要它......一旦我选择了具有最高利润值的对象(从副本中),我基本上把其他的都扔掉了。
-
很高兴有帮助。 Firebase 是一个不同的主题,但它在这里提供的答案可能是对数据结构的一个很好的反思(我的意思是,它让我对数据结构和我们都有的习惯有了不同的看法)看看这个答案是你在找什么顺便说一句:stackoverflow.com/questions/16747798/…
标签: arrays node.js duplicates filtering lodash