通过从现有 JSON 中过滤来创建 JSON

Creating a JSON by filtering from an existing JSON

我正在尝试通过从父节点 JSON 过滤父节点名称和其中的几个子节点来创建 JSON。这样做时出现以下错误

Cannot read property 'filter' of undefined at data.children.filter.map.Object.assign.children.o.children.reduce

https://jsfiddle.net/snt1/nLua0oob/13/

这是你想要的吗?

result = data.children
    .filter(o => o.name === 'Age')
    .map(o => Object.assign(
        {},
        o,
        {
            children: o.children.filter(o => o.name === 'Male' || o.name === 'Female')
        }
    ));

console.log(result);

我只能在您的数据中找到每个字符串 "Male""Female" 的一个实例,这将它们提取出来。