简体   繁体   English

JS 从对象数组中过滤某些部分

[英]JS filter some part from an array of objects

I've this array.我有这个数组。

const routes = [
{
    path:'/dashboard',
    text: "Dashboard"
},
{
    path:'/disputes',
    text: "Disputes"
},
{
    children: [
        {
          text: "Create Suburb",
          path: "/create-suburb"
        },
        {
          text: "View and Update Suburb",
          path: "/view-suburb"
        }
      ]
},
{
    children: [
        {
          text: "Create user",
          path: "/create-user"
        },
        {
          text: "View and Update users",
          path: "/view-users"
        }
      ]
}

] ]

and I've this array我有这个数组

const permissions = ['/dashboard','/view-suburb'];

What I want is filter out objects from the array where there is not in the permissions array.我想要的是从数组中过滤掉permissions数组中没有的对象。

My expected out put is this我的预期输出是这个

const routes = [
    {
        path:'/dashboard',
        text: "Dashboard"
    },

    {
        children: [
            {
              text: "View and Update Suburb",
              path: "/view-suburb"
            }
          ]
    },

]

Note that two objects are completely removed and some part of the third object also removed.请注意,两个对象被完全删除,第三个 object 的某些部分也被删除。 How do I achieve this using JS?我如何使用 JS 实现这一点?

What I've done upto now is this我到目前为止所做的是这个

items.filter(e=>{
    if(e.path){
        return permissions.includes(e.path)
    }else{

    }
})

Hope my question is clear to you.希望我的问题对你来说很清楚。

You could do it with a reduce - filter alone won't work here as you're actually transforming child arrays rather than purely filtering the top level array items您可以使用 reduce 来做到这一点 - 单独的过滤器在这里不起作用,因为您实际上是在转换子 arrays 而不是纯粹过滤顶级数组项


routes.reduce((result, route) => {
  const { path, children } = route;
  if (children) {
    const filteredChildren = children.filter(child => permissions.includes(child.path));

    // case where some child routes match permissions
    if (filteredChildren.length !== 0) {
        return [ ...result, { ...route, children: filteredChildren }];
    } 
  }

  // case where path is present and permissions includes path
  if (path && permissions.includes(path)) {
      return [ ...result, route ];
  }

  // case where there's no match between path and permissions 
  return result;
}, []);

Try this!!尝试这个!!

 const routes = [ { path:'/dashboard', text: "Dashboard" }, { path:'/disputes', text: "Disputes" }, { children: [ { text: "Create Suburb", path: "/create-suburb" }, { text: "View and Update Suburb", path: "/view-suburb" } ] }, { children: [ { text: "Create user", path: "/create-user" }, { text: "View and Update users", path: "/view-users" } ] } ] const permissions = ['/dashboard','/view-suburb']; let result = []; permissions.map(permission=>{ routes.map(route=>{ if(route.hasOwnProperty('children')){ route.children.map((r,i)=>{ if(r.path == permission){ route.children = route.children.splice(i); route.children = route.children.slice(-1); result.push(route) } }); }else{ if(route.path == permission){ result.push(route) } } }); }) console.log(result)

This one also worked for me.这个也对我有用。

    var newData = [];

    $.each(routes, function (key, value) {
        debugger
        var data = this;
        if (data.path) {
            if (permissions.includes(data.path)) {
                newData.push(data);
            }
        }
        else {
            var data2 = data;
            $.each(data2, function (key, value1) {

                $.each(value1, function (key, value) {

                    var data = value;
                    if (data.path) {
                        if (permissions.includes(data.path)) {
                            newData.push(data);
                        }
                    }
                });
           });
        }
    })

Actually you're on the right path.其实你是在正确的道路上。 A little modification is required in your approach to solving the problem and the code you've already written will give you the desired result.解决问题的方法需要稍作修改,您已经编写的代码将为您提供所需的结果。

Observe carefully: your children inside routes array is a replica of roue itself.仔细观察:您的孩子在 routes 数组中是 roue 本身的复制品。

So, what you need to do is:所以,你需要做的是:

  1. Put the logic you've already written for filtering an array of objects in a separate function and modify it such a way that it takes an object as an argument, check for the existence of the "path" key in the object and return true if permissions.includes(argumentObject.path)将您已经编写的用于过滤对象数组的逻辑放在单独的 function 中,并对其进行修改,使其将 object 作为参数,检查 ZA8CFDE6331BD4B62AC96F891 中是否存在“路径”键并返回 true if权限.includes(argumentObject.path)
  2. Loop ove the routes array循环路由数组
  3. If your iterated object hasOwnProperty('children') then call the function with the objects of children array as argument如果您的迭代 object hasOwnProperty('children') 然后调用 function 与 children 数组的对象作为参数
  4. Else call the function with the objects of routes array as an argument否则以路由数组的对象作为参数调用 function

Hope this algorithm makes sense, let me know if you need any help with the code.希望这个算法有意义,如果您需要任何代码帮助,请告诉我。

Ideally, you should check the access to the route inside the canActivate guard and navigate the user further to the appropriate route.理想情况下,您应该检查对 canActivate 守卫内路由的访问,并将用户进一步导航到适当的路由。

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

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