【发布时间】:2022-01-20 19:15:57
【问题描述】:
我有这个嵌套对象:
const menus = {
path: '/actions/step',
icon: 'fa fa-wine-bottle',
title: 'Fasi',
children: [
{
path: '/actions/step/analysis',
title: 'Analisi'
},
{
path: '/actions/step/import',
title: 'Importazione'
},
{
path: '/actions/step/squeeze',
title: 'Spremitura'
},
{
path: '/actions/step/move',
title: 'Spostamento'
},
{
path: '/actions/step/splitauto',
title: 'Travaso Guidato'
},
{
path: '/actions/step/stir',
title: 'Rimestaggio'
},
{
path: '/actions/step/clarify',
title: 'Chiarifica'
},
{
path: '/actions/step/stabilization',
title: 'Stabilizzazione'
},
{
path: '/actions/step/bottling',
title: 'Imbottigliamento'
},
{
path: '/actions/step/clean',
title: 'Pulizia'
},
{
path: '/actions/step/clean_close',
title: 'Pulizia e Chiusura'
},
{
path: '/actions/step/add_product',
title: 'Aggiunta Prodotto'
},
]
},
{
path: '/actions',
icon: 'fa fa-tasks',
title: 'Azioni',
children: [
{
path: '/actions/type',
title: 'Tipi di Azione'
},
{
path: '/actions/list',
title: 'Lista delle Azioni'
},
{
path: '/actions/traceability',
title: 'Tracciabilità'
}
]
},
{
path: '/warehouse',
icon: 'fa fa-warehouse',
title: 'Magazzino',
children: [
{
path: '/warehouse/list-warehouse-item',
title: 'Lista Oggetti',
children: [
{
path: '/warehouse/new-warehouse-item',
title: 'Nuovo Oggetto'
}
]
},
]
},
{
path: '/suppliers',
icon: 'fa fa-truck',
title: 'Fornitori',
children: [
{
path: '/suppliers/list-suppliers',
title: 'Lista Fornitori',
children: [
{
path: '/suppliers/new-supplier',
title: 'Nuovo Fornitore'
}
]
}
]
}
我想要实现的是根据路径值过滤嵌套对象。
如果我有/actions/step/import,我想拥有这个:
[{
path: '/actions/step',
icon: 'fa fa-wine-bottle',
title: 'Fasi'
},
{
path: '/actions/step/import',
title: 'Importazione'
}]
或者如果/warehouse/new-warehouse-item 我会:
[{
path: '/warehouse',
icon: 'fa fa-warehouse',
title: 'Magazzino'
},
{
path: '/warehouse/list-warehouse-item',
title: 'Lista Oggetti'
},
{
path: '/warehouse/new-warehouse-item',
title: 'Nuovo Oggetto'
}]
我试图做的是像这样过滤,但它不完整(this.$router.history.current.path 包含字符串路径):
menus.filter(menu => {
if(menu.children){
return menu.children.some(child => {
if(child.children){
return child.children.some(nephew => {
return nephew.path === this.$router.history.current.path;
})
} else {
return child.path === this.$router.history.current.path;
}
})
} else {
return menu.path === this.$router.history.current.path;
}
});
【问题讨论】:
-
您的意思是要查找给定对象的父对象吗?
-
是的,创建一个新的清理对象也很好
标签: javascript arrays object filter