javascript数组中的深层过滤对象

编程入门 行业动态 更新时间:2024-10-12 03:21:13
本文介绍了javascript数组中的深层过滤对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

从下面的代码中,我只需要返回包含 meta.menu 的对象,同时保留结构.

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

所以在下面的例子中,只有带有 "panel.users.view" 的对象会被删除.我已经尝试制作一个递归函数来遍历它,但我无法弄清楚如何无限地遍历它,因为可能有更多的对象.我不知道一个路由对象可以达到多少层.

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.

我还查看了 lodashcollect.js 以查看是否有可用于此目的的函数,但我无法找到解决方案我自己的.

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: {},
                    },
                ],
            }
        ],
    }
];

推荐答案

你可以想象一个递归函数,看起来像这样.

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

这篇关于javascript数组中的深层过滤对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 00:25:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   对象   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!