2016-09-29 107 views
-1

我有一个对象像下面我想删除所有键值其中key =“/”下划线过滤对象

let routes = { 
    '/dashboard': { 
     name : 'Dashboard', 
     component : appDashboard, 
     icon: 'fa fa-dashboard', 
     subRoutes: { 
      '/': { 

       component:appDashEcommerce 
      }, 
      '/ecommerce': { 
       name : 'Ecommerce', 
       component:appDashEcommerce 
      }, 

     } 

    }, 
    '/apps': { 
     name : 'Apps', 
     component : appAppsPage, 
     icon : 'fa fa-th', 
     subRoutes: { 
      '/': { 

       component:appInbox 
      }, 
      '/mailbox': { 
       name : 'maibox', 
       component : appInbox, 
       icon : 'fa fa-th', 
      } 
     } 
}; 

我当前的代码

var ret2 = _.omit(routes, function(val, key, object) { 
     if(_.has(val , 'subRoutes')){ 
      _.omit(val.subRoutes , function(v, k, o) { 
       return key === '/' 
      }) 
      }else{ 

      return key === '/' || key === '*' 
     } 
     }) 

    console.log(ret2) 
+2

难道你有问题还是你刚刚说了你的胸部。 – 2016-09-29 21:47:52

+0

对不起,这是一个问题,你有答案吗? –

+1

我有一个,但它更简单,因为它不使用整个库来实现简单的事情 - > https://jsfiddle.net/qspunug6/1/ – adeneo

回答

1

我相信你使用你的内部函数的错误变量。

编辑:对不起,我懒惰,试图写没有测试。上述论断是正确的,但也有一些其他错误以及,解决在更新的代码如下

https://jsfiddle.net/e708gyna/

//_.omitBy should be used for functions according to the lodash spec 
var ret2 = _.omitBy(routes, function(val, key, object) { 
    if(_.has(val , 'subRoutes')) { 
    //In order to use the result below, we need to store it 
    val.subRoutes = _.omitBy(val.subRoutes , function(v, k, o) { 
     //Since you're running this on the subRoutes 
     //you need to test the key that you've defined 
     //for this inner handler 
     return (k === '/'); 
    }) 
    } else { 
    return (key === '/' || key === '*'); 
    } 
}) 
+0

不幸的是,这似乎并没有工作 –

+1

固定。在第一次运行中我还没有修复其他一些错误。 –

+0

@ dominic-aquiliana这修复它谢谢 –