0

I have an array with many objects (Drugs). each object has 3 keys: "Drug1", "Drug2", and riskingRate. How can i remove duplicated objects if Drug1 and Drug2 values are same (riskingRate is not important in finding duplicats). then keep object with higher riskingRate and remove others. here is a simple array:

var Warfarin = [{
    "Drug1": "Warfarin",
    "Drug2": "vitamin K",
    "riskRating": "C",
}, {
    "Drug": "aspirin",
    "Drug2": "MiFEPRIStone",
    "riskRating": "C",
}, {
    "Drug1": "Warfarin",
    "Drug2": "Omacetaxine",
    "riskRating": "X",
}, {
    "Drug1": "Warfarin",
    "Drug2": "vitamin K",
    "riskRating": "X",
},{
    "Drug1": "Warfarin",
    "Drug2": "vitamin K",
    "riskRating": "A",
}]

in this example objects 0, 3 and 4 are duplicated and the object 3 has a higher riskingRate. (riskingRate X>D>C>B>A). how can I remove objects 0 and 4 and keep objects 1,2 and 3. Thanks.

3

2 Answers 2

3

You can do something like this:

var weights = ["X", "D", "C", "B", "A"];

var Warfarin = [{
    "Drug1": "Warfarin",
    "Drug2": "vitamin K",
    "riskRating": "C",
}, {
    "Drug": "aspirin",
    "Drug2": "MiFEPRIStone",
    "riskRating": "C",
}, {
    "Drug1": "Warfarin",
    "Drug2": "Omacetaxine",
    "riskRating": "X",
}, {
    "Drug1": "Warfarin",
    "Drug2": "vitamin K",
    "riskRating": "X",
},{
    "Drug1": "Warfarin",
    "Drug2": "vitamin K",
    "riskRating": "A",
}];

var response = Warfarin.sort((v1, v2) => (weights.indexOf(v1.riskRating) - weights.indexOf(v2.riskRating)))
.reduce((a, c)=>{
    if(!a.some(v => (v.Drug1 == c.Drug1 && v.Drug2 == c.Drug2))){
        a.push(c);
    }
    return a;
}, []);

console.log(response);

What this does is to first sort the array based on the objects' riskRating so the higher risk objects will be at the start, then just reduce the array and don't include the duplicates in the resulting array.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi. thanks for your help. console: ReferenceError: weights is not defined
@johnsadeghi You're missing the weights variable that I've included at the start of my snippet.
3

Here is a solution without sorting, but with an ES6 Map, which would make it run in O(n) time instead of O(nlogn):

const Warfarin = [{"Drug1": "Warfarin","Drug2": "vitamin K","riskRating": "C",}, {"Drug": "aspirin","Drug2": "MiFEPRIStone","riskRating": "C",}, {"Drug1": "Warfarin","Drug2": "Omacetaxine","riskRating": "X",},{"Drug1": "Warfarin","Drug2": "vitamin K","riskRating": "X",},{"Drug1": "Warfarin","Drug2": "vitamin K","riskRating": "A",}];

const result = Array.from(Warfarin.reduce ( (acc, obj) => {
    const key = JSON.stringify([obj.Drug1,obj.Drug2]);
    return !acc.has(key) || acc.get(key).riskRating < obj.riskRating
        ? acc.set(key, obj) : acc; 
}, new Map).values());

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

Why JSON.stringify([obj.Drug1,obj.Drug2]) as the key, and not just [obj.Drug1,obj.Drug2], since Maps don't require keys to be strings?
Because if you pass a new array as key, it will always be considered different (comparison will be by reference).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.