Home > OS >  Filter and Combine Nested Array in ES6
Filter and Combine Nested Array in ES6

Time:12-02

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)

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

You can use Array.flatMap() to add the subGroups data

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

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),...oldGroup.flatMap((group) => group.subGroups)])]

console.log(newGroup)

CodePudding user response:

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"]
  • Related