首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript或rxjs递归过滤嵌套json数据

使用javascript或rxjs递归过滤嵌套json数据
EN

Stack Overflow用户
提问于 2020-04-15 08:58:16
回答 1查看 432关注 0票数 1

我希望找到优雅的解决方案来过滤嵌套的json数据结构,以便可以应用过滤器功能。

pre-condtions:

recursive.

  • Filter的深度未知(需要递归求解),如果值不匹配,并且有子值,则应该比所有的子值都附加到父节点,而且
  • 的工作深度应该不止一个,我的意思是,如果按YZ选择过滤器,那么Y和Z的结果应该显示在树结构中。但是,下面显示的kind属性筛选器示例和应用筛选的值是Y .

谢谢!!

e.g

代码语言:javascript
复制
   var dataToFilter = {
        "children": [{
            "tagPath": "/../../tagPath-1",
            "children": [{
                "tagPath": "/../../tagPath-1-1",
                "children": [{
                        "tagPath": "/../../tagPath-1-2",
                        "kind": "Y",
                        "children": [{
                                "tagPath": "/../../tagPath-1-3.1",
                                "kind": "X",
                                "children": []
                            },
                            {
                                "tagPath": "/../../tagPath-1.3.2",
                                "kind": "X",
                                "children": [{
                                    "tagPath": "/../../tagPath-1.3",
                                    "kind": "Y",
                                    "children": []
                                }]
                            }
                        ]
                    },
                    {
                        "kind": "Y",
                        "children": []
                    }
                ],
                "kind": "X",
            }],
            "kind": "Y",
        }]

当值为Y时,对Y属性的期望输出:

代码语言:javascript
复制
var desiredOutput = {
        "children": [{
            "tagPath": "/../../tagPath-1",
            "children": [{
                    "tagPath": "/../../tagPath-1-2",
                    "kind": "Y",
                    "children": [{
                        "tagPath": "/../../tagPath-1.3",
                        "kind": "Y",
                        "children": []
                    }]
                },
                {
                    "kind": "Y",
                    "children": []
                }
            ],
            "kind": "Y",
        }]
    ```
EN

回答 1

Stack Overflow用户

发布于 2020-04-17 07:50:56

您可以使用以下函数:

代码语言:javascript
复制
const restructure = (nodes, filter) => nodes.reduce(
    (acc, node) => {
        const children = restructure(node.children, filter);

        return acc.concat(
            filter(node)
                ? {...node, children}
                : children
        );
    },
    []
);

它接受filter作为函数。这使您可以决定如何过滤,例如:

代码语言:javascript
复制
const yOnly = restructure(dataToFilter, node => 'Y' === node.kind);

或者:

代码语言:javascript
复制
const yAndX = restructure(dataToFilter, node => ['Y', 'X'].includes(node.kind));

上面代码的唯一不同之处在于,dataToFilter必须已经是一个节点数组(请参阅下面的完整代码片段)。

代码语言:javascript
复制
const restructure = (nodes, filter) => nodes.reduce(
    (acc, node) => {
        const children = restructure(node.children, filter);

        return acc.concat(
            filter(node)
                ? {...node, children}
                : children
        );
    },
    []
);

const dataToFilter = [{
    'tagPath':  '/../../tagPath-1',
    'children': [{
        'tagPath':  '/../../tagPath-1-1',
        'children': [{
            'tagPath':  '/../../tagPath-1-2',
            'kind':     'Y',
            'children': [{
                'tagPath':  '/../../tagPath-1-3.1',
                'kind':     'X',
                'children': []
            }, {
                'tagPath':  '/../../tagPath-1.3.2',
                'kind':     'X',
                'children': [{
                    'tagPath':  '/../../tagPath-1.3',
                    'kind':     'Y',
                    'children': []
                }]
            }]
        }, {
            'kind':     'Y',
            'children': []
        }],
        'kind':     'X',
    }],
    'kind':     'Y',
}];

const yOnly = restructure(dataToFilter, node => 'Y' === node.kind);
const xOnly = restructure(dataToFilter, node => 'X' === node.kind);
const yAndX = restructure(dataToFilter, node => ['Y', 'X'].includes(node.kind));

console.log(yOnly);
console.log(xOnly);
console.log(yAndX);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61224909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档