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?

Jack N
  • 175
  • 1
  • 10

1 Answers1

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)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73