javascript ES6中的过滤和合并嵌套数组

aoyhnmkz  于 2022-12-02  发布在  Java
关注(0)|答案(4)|浏览(289)

我需要把gidsubGroups组合起来,输出到一个没有重复的字符串数组中,我现在的问题是它只得到第一层。

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)
gwo2fgha

gwo2fgha1#

.flatMap代替.map,在回调中返回一个数组--.gid的值,并同样在.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.flatMap((group) => [group.gid, ...group.subGroups]))]

console.log(newGroup)
xmjla07d

xmjla07d2#

有几种方法可以实现这一点,下面是.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)
z4bn682m

z4bn682m3#

可以使用Array.flatMap()添加子组数据

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)
rryofs0p

rryofs0p4#

可以使用Array.prototype.map()和Array.prototype.flat()方法将oldGroup数组转换为仅包含gid和subGroups值的新数组。然后可以使用Array.prototype.filter()方法删除新数组中的任何重复值。

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

相关问题