1

From the following code below I need to return only the objects that have meta.menu in them while preserving the structure.

So in the case below, only the object with "panel.users.view" would get removed. 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.

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: {},
                    },
                ],
            }
        ],
    }
];
3
  • 1
    Hello. Your "panel.users" path , haven't meta.menu. So should all of it be completely removed with children? Commented Feb 19, 2020 at 3:30
  • Hi @ali.rezaie, thanks for taking the time to look into this. No, I want to preserve it if it has children. In this case, only the object with the name "panel.users.view" would get removed. Commented Feb 19, 2020 at 3:44
  • From the following code below I need to return only the objects that have meta.menu in them while preserving the structure - this will give []. You may want to reconsider the presence of meta.menu in parent routes because otherwise you have to include them retroactively if their children are main, this certainly doesn't make things easier. What did you try? Since you're new to SO, you may want to know 'write the code for me' questions that show no efforts aren't well-received. Commented Feb 19, 2020 at 8:06

1 Answer 1

-2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hello! Thanks for taking the time to write this. I have a small issue with it though, this solution results in RangeError: Maximum call stack size exceeded.
@user12922812 Okay sorry I did it without really testing here is a working solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.