3

I need to combine the gid and subGroups and output it into an array of strings without duplication. My problem now is that it only gets the first level.

const oldGroup = [
    {
        "gid": "JFK",
        "subGroups": [
            "SFO",
            "LAX"
        ]
    },
    {
        "gid": "JFK",
        "subGroups": [
            "SFO",
            "LAX"
        ]
    },
    {
        "gid": "SFO",
        "subGroups": []
    },
    {
        "gid": "LAX",
        "subGroups": [
            "LGA"
        ]
    }
]

const newGroup = [...new Set(oldGroup.map((group) => group.gid))]

console.log(newGroup)

1
  • 1
    What’s the expected result? Are you looking for const newGroup = Array.from(new Set(oldGroup.flatMap(({ gid, subGroups }) => subGroups.map((item) => `${gid} → ${item}`))))? Commented Dec 2, 2022 at 4:07

3 Answers 3

2

There are several ways to achieve this, here is one with .reduce():

const oldGroup = [
    { "gid": "JFK", "subGroups": [ "SFO", "LAX" ] },
    { "gid": "JFK", "subGroups": [ "SFO", "LAX" ] },
    { "gid": "SFO", "subGroups": [] },
    { "gid": "LAX", "subGroups": [ "LGA" ] }
];

const newGroup = Object.keys(oldGroup.reduce((acc, obj) => {
  acc[obj.gid] = true;
  obj.subGroups.forEach(name => { acc[name] = true; } );
  return acc;
}, {}));

console.log(newGroup)

Explanation:

  • The .reduce() has two parameters:
    • a function parameter with two options acc, obj. The former is the accumulator that keeps track of the data needed, the latter is the array item object
    • an initial setting, and empty {} object in our case
  • Object.keys() is applied on the accumulated data
Sign up to request clarification or add additional context in comments.

Comments

1

With .flatMap instead of .map, return an array in the callback - the .gid value, and also spread in the .subGroups array.

const oldGroup = [
    {
        "gid": "JFK",
        "subGroups": [
            "SFO",
            "LAX"
        ]
    },
    {
        "gid": "JFK",
        "subGroups": [
            "SFO",
            "LAX"
        ]
    },
    {
        "gid": "SFO",
        "subGroups": []
    },
    {
        "gid": "LAX",
        "subGroups": [
            "LGA"
        ]
    }
]

const newGroup = [...new Set(oldGroup.flatMap((group) => [group.gid, ...group.subGroups]))]

console.log(newGroup)

3 Comments

Array.from(new Set(...)) maybe?
That's equivalent to [...new Set()] here
It seems harder to read, that might just be me.
0

You can use the Array.prototype.map() and Array.prototype.flat() methods to transform the oldGroup array into a new array that only contains the gid and subGroups values. You can then use the Array.prototype.filter() method to remove any duplicate values from the new array.

const oldGroup = [
    {
        "gid": "JFK",
        "subGroups": [
            "SFO",
            "LAX"
        ]
    },
    {
        "gid": "JFK",
        "subGroups": [
            "SFO",
            "LAX"
        ]
    },
    {
        "gid": "SFO",
        "subGroups": []
    },
    {
        "gid": "LAX",
        "subGroups": [
            "LGA"
        ]
    }
]
// Transform the oldGroup array into a new array that only contains the gid and subGroups values
const newGroup = oldGroup.map(group => [group.gid, ...group.subGroups]).flat();

// Remove any duplicate values from the new array
const dedupedGroup = newGroup.filter((value, index) => newGroup.indexOf(value) === index);

console.log(dedupedGroup); // ["JFK", "SFO", "LAX", "SFO", "LAX", "SFO", "LAX", "LGA"]

Comments

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.