topic description
there are 2 arrays
related codes
var a = ["customer","supplier","materal","purchaseOrder","rolesMenge"]
var b = [
{
name:"maindata",
children:[
{
name:"customer",
meta:{
title:"customer"
}
},
{
name:"supplier",
meta:{
title:"supplier"
}
},
{
name:"materal",
meta:{
title:"materal"
}
},
]
},
{
name:"purchase",
children:[
{
name:"purchaseOrder",
meta:{
title:"purchaseOrder"
}
},
{
name:"purchaseGood",
meta:{
title:"purchaseGood"
}
},
]
},
{
name:"stock",
children:[
{
name:"stockOrder",
meta:{
title:"stockOrder"
}
}
]
},
{
name:"config",
children:[
{
name:"userConfig",
children:[
{
name:"rolesMenge",
meta:{
title:"rolesMenge"
}
}
]
},
]
}
]
my code
function getarr(a,b){
return b.reduce((k,m) => {
if(m.children){
let obj = {
name:m.name,
children:[]
}
for(let j of m.children){
if(j.children){
getarr(a,m.children)
} else {
if(a.includes(j.meta.title)){
obj.children.push(j)
}
}
}
if(obj.children.length){
k.push(obj)
}
}
return k
},[])
}
what result do you expect? What is the error message actually seen?
want to get
[
{
name: "maindata",
children:[
{
name:"customer",
meta:{
title:"customer"
}
},
{
name:"supplier",
meta:{
title:"supplier"
}
},
{
name:"materal",
meta:{
title:"materal"
}
}
]
},
{
name:"purchase",
children:[
{
name:"purchaseOrder",
meta:{
title:"purchaseOrder"
}
}
]
},
{
name:"config",
children:[
{
name:"userConfig",
children:[
{
name:"rolesMenge",
meta:{
title:"rolesMenge"
}
}
]
},
]
}
]