I'd need your help... I'm sorry if the question has already been asked but I can't seem to find a suitable answer for my problem: I'm trying to extract (not remove) the list of the duplicates from my array.
ultimately, the main objective is to keep only one of the duplicated objects (in the array) with the higher profit...
Here's a simple example of my array:
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
}
];
And I managed to extract a list of almost all duplicates using:
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));
}
}
And here's the result:
[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
}
The Lodash method (_.uniqBy) is keeping one of the duplicates in the main Array, and, in order to ultimately get the best (_.maxBy(arr, 'profit')
) of the duplicates, I'd need it with the other duplicates.
I'm not sure I'm very clear, but if you need any clarification please let me know!
Thanks in advance to you all!
********** EDIT ************* As suggested by stasovlas you'll find below the expected result and why the other objects in the array were removed:
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
}
];