Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS从对象数组中筛选某些部分_Javascript - Fatal编程技术网

Javascript JS从对象数组中筛选某些部分

Javascript JS从对象数组中筛选某些部分,javascript,Javascript,我有这个数组 const routes = [ { path:'/dashboard', text: "Dashboard" }, { path:'/disputes', text: "Disputes" }, { children: [ { text: "Create Suburb", path: "/create-suburb" }, { tex

我有这个数组

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'];
我想要的是从
权限
数组中没有的数组中筛选出对象

我的预期产量是这个

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

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

]
请注意,两个对象被完全删除,第三个对象的某些部分也被删除。如何使用JS实现这一点

到目前为止我所做的是

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

    }
})

希望我的问题对您来说是清楚的。

理想情况下,您应该检查对canActivate guard内部路由的访问,并将用户导航到适当的路由。

您可以使用reduce筛选器来完成此操作-仅使用reduce筛选器无法在这里工作,因为您实际上是在转换子数组,而不是纯粹过滤顶级数组项


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;
}, []);
试试这个

const路由=[
{
路径:'/dashboard',
文本:“仪表板”
},
{
路径:“/争议”,
案文:“争端”
},
{
儿童:[
{
文本:“创建郊区”,
路径:“/创建郊区”
},
{
文本:“查看和更新郊区”,
路径:“/查看郊区”
}
]
},
{
儿童:[
{
文本:“创建用户”,
路径:“/create user”
},
{
文本:“查看和更新用户”,
路径:“/查看用户”
}
]
}
]
常量权限=['/dashboard','/view'];
让结果=[];
权限映射(权限=>{
routes.map(route=>{
if(route.hasOwnProperty('children')){
路线.儿童.地图((r,i)=>{
if(r.path==权限){
route.children=route.children.splice(i);
route.children=route.children.slice(-1);
结果:推送(路线)
}
});
}否则{
if(route.path==权限){
结果:推送(路线)
}
}
});
})

console.log(result)
这一个也适用于我

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


同意是否有角度-不确定是否有角度是的,这不是角度。这是VueJS