下划线过滤对象
Underscore filter an object
我有一个如下所示的对象,我想删除所有键值,其中键 = '/'
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)
我认为您为内部函数使用了错误的变量。
编辑:抱歉,我偷懒了,试图在没有测试的情况下编写。上面的断言是正确的,但也有一些其他错误,在下面更新的代码中解决
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 === '*');
}
})
我有一个如下所示的对象,我想删除所有键值,其中键 = '/'
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)
我认为您为内部函数使用了错误的变量。
编辑:抱歉,我偷懒了,试图在没有测试的情况下编写。上面的断言是正确的,但也有一些其他错误,在下面更新的代码中解决
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 === '*');
}
})