gpt4 book ai didi

arrays - NodeJS : Extracting duplicates from Array

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:20 24 4
gpt4 key购买 nike

我需要你的帮助...如果问题已经被问到,我很抱歉,但我似乎找不到适合我的问题的答案:我正在尝试提取(而不是删除) )数组中重复项的列表。

最终,主要目标是仅保留一个具有较高利润的重复对象(在数组中)...

这是我的数组的一个简单示例:

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
}
];

最佳答案

我不确定我的问题理解,但这是我的解决方案:

const res = _.reduce(arr, (result, item) => {
const same = _.find(result, r => _.some([
r.mkBase === item.mkBase,
r.mkComp === item.mkComp
])); // find same already added item

if (same === undefined) {
return _.concat(result, item); // just push item
}

if (same.invest.profit >= item.invest.profit) {
return result; // do nothing if profit is less then already added
}

return _.chain(result) // remove item with smaller profit and push item with higher profit
.reject({ mkBase: same.mkBase, mkComp: same.mkComp })
.concat(item)
.value();
}, []);

关于arrays - NodeJS : Extracting duplicates from Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48325313/

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