简体   繁体   English

javascript数组中的深层过滤对象

[英]Deep filter objects in javascript array

From the following code below I need to return only the objects that have meta.menu in them while preserving the structure.从下面的代码中,我只需要返回包含meta.menu的对象,同时保留结构。

So in the case below, only the object with "panel.users.view" would get removed.所以在下面的情况下,只有带有"panel.users.view"的对象会被删除。 I've tried making a recursive function to traverse it but I can't figure out how to infinitely traverse it because there could be more objects.我已经尝试制作一个递归函数来遍历它,但我无法弄清楚如何无限地遍历它,因为可能有更多的对象。 I don't know how many levels deep a routes object can get.我不知道一个路由对象可以达到多少级。

I've also looked through lodash and collect.js to see if there were functions that could be used for this but I'm unable to find a solution on my own.我还查看了lodashcollect.js以查看是否有可用于此目的的函数,但我无法自己找到解决方案。

Would greatly appreciate the help, many thanks!非常感谢您的帮助,非常感谢!

const routes = [
    {
        path: "/panel",
        name: "panel",
        redirect: { name: "panel.dashboard" },
        component: {},
        children: [
            {
                path: "/dashboard",
                name: "panel.dashboard",
                component: {},
                meta: {
                    menu: "main"
                },
            },
            {
                path: "/users",
                name: "panel.users",
                redirect: { name: "panel.dashboard" },
                component: {},
                children: [
                    {
                        path: "list",
                        name: "panel.users.list",
                        component: {},
                        meta: {
                            menu: "main"
                        },
                    },
                    {
                        path: "view/:user_id",
                        name: "panel.users.view",
                        component: {},
                    },
                ],
            }
        ],
    }
];

You could imagine a recursive function which would look something like.你可以想象一个递归函数,它看起来像。

const res = [];
function findMeta(rs) {
  rs.forEach(r => {
      for (let [key, value] of Object.entries(r)) {
        //console.log(`${key}`, value);
                if(key === "children") findMeta(value);
                if(key === "meta") res.push(r);
      }
  })
  return res;
}

 const routes = [ { path: "/panel", name: "panel", redirect: { name: "panel.dashboard" }, component: {}, children: [ { path: "/dashboard", name: "panel.dashboard", component: {}, meta: { menu: "main" }, }, { path: "/users", name: "panel.users", redirect: { name: "panel.dashboard" }, component: {}, children: [ { path: "list", name: "panel.users.list", component: {}, meta: { menu: "main" }, }, { path: "view/:user_id", name: "panel.users.view", component: {}, }, ], } ], } ]; const res = []; function findMeta(rs) { rs.forEach(r => { for (let [key, value] of Object.entries(r)) { //console.log(`${key}`, value); if(key === "children") findMeta(value); if(key === "meta") res.push(r); } }) return res; } console.log(findMeta(routes))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM