1

I am trying to format my array that I created from a csv file. After parsing it into the correct format, I need a way to remove duplicates that I run into from the csv file.

Here is an example of the array that I have

[{
    "pccs": ["1ADA"],
    "markets": [{
        "origin": "LAX",
        "destination": "DFW"
    }, {
        "origin": "LAX",
        "destination": "IAH"
    }, {
        "origin": "LAX",
        "destination": "IAH"
    }, {
        "origin": "LAX",
        "destination": "IAH"
    }]
}, {
    "pccs": ["1ABA"],
    "markets": [{
        "origin": "LAX",
        "destination": "JFK"
    }]
}]

I am trying to format this to only display a unique set for each element in the list to get a response like this:

[{
    "pccs": ["1ADA"],
    "markets": [{
        "origin": "LAX",
        "destination": "DFW"
    }, {
        "origin": "LAX",
        "destination": "IAH"
    }]
}, {
    "pccs": ["1ABA"],
    "markets": [{
        "origin": "LAX",
        "destination": "JFK"
    }]
}]

I'm leaning towards using the filter() function, but not sure how to implement it with a list of objects. Would i have to created a nested loop to access each individual element?

3
  • i'm not a pro but i think ya you need a double loop to compare Commented Jun 5, 2019 at 14:41
  • Both the keys origin and destination should be compared or only destination? Commented Jun 5, 2019 at 14:42
  • @MaheerAli Both of the keys should be compared Commented Jun 5, 2019 at 14:45

1 Answer 1

0

You can create a helper function to compare two objects using every(). Then use filter() on the markets of each element of array and remove the desired elements.

Note: I have used forEach and code will modify original array because I think that you need to modify original if that's not the case ask me I will add other solution.

const arr = [{ "pccs": ["1ADA"], "markets": [{ "origin": "LAX", "destination": "DFW" }, { "origin": "LAX", "destination": "IAH" }, { "origin": "LAX", "destination": "IAH" }, { "origin": "LAX", "destination": "IAH" }] }, { "pccs": ["1ABA"], "markets": [{ "origin": "LAX", "destination": "JFK" }] }]

const compObjs = (obj1, obj2) => Object.keys(obj1).every(k => obj1[k] === obj2[k])
arr.forEach(x => {
  x.markets = x.markets.filter(a => x.markets.find(b => compObjs(b,a)) === a)
})
console.log(arr)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.