如何从嵌套数组对象中获取基于层次结构的名称?
How can I get hierarchy based name from nested array objects?
I have an array of objects and want to get data from child to parent by their respected name like, if we have an array with multiple objects then I want:
- 主页
- 首页 >> 关于
- 主页 >> 博客
- 首页>>服务
- 首页 >> 服务 >> React
- 首页 >> 服务 >> PHP
- 首页 >> 服务 >> PHP >> 蛋糕
- 首页 >> 服务 >> PHP >> 蛋糕 >> V7
- 首页 >> 服务 >> PHP >> Laravel
- 首页 >> 服务 >> AIML
- 首页 >> 服务 >> ROR
- 首页 >> 联系我们
代码如下:
[{
"id": "5e27f3ded2e5e90001b4b989",
"title": "Home",
"children": [{
"id": "5e27f3dfd2e5e90001b4b993",
"title": "About Us",
"children": []
},
{
"id": "5e27f3dfd2e5e90001b4b996",
"title": "Contact Us",
"children": []
},
{
"id": "5e574a55b4895e0001313670",
"title": "Services",
"children": [{
"id": "5e574a63b4895e0001313677",
"title": "php",
"children": [{
"id": "5e574a8ab4895e000131368d",
"title": "Cake php",
"children": [{
"id": "5e574ad1b4895e000131369c",
"title": "version 7",
"children": []
}]
},
{
"id": "5e574aa2b4895e0001313695",
"title": "CodeIgniter",
"children": []
}
]
},
{
"id": "5e574a70b4895e000131367f",
"title": "React",
"children": []
},
{
"id": "5e574a7ab4895e0001313686",
"title": "Ruby on Rails",
"children": []
}
]
}
]
},
{
"id": "",
"title": "Orphan",
"children": [{
"id": "5e54a3dd7924a50001df0bd3",
"title": "New Page",
"children": []
}]
}
]
作为回应,我想要如下对象数组中的数据:
[{
label: '5e27f3ded2e5e90001b4b989',
value: 'Home'
},
{
label: '5e27f3dfd2e5e90001b4b993',
value: 'Home >> About Us"'
},
{
label: '5e27f3dfd2e5e90001b4b996',
value: 'Home >> Contact Us'
},
{
label: '5e574a55b4895e0001313670',
value: 'Home >> Services'
},
{
label: '5e574a63b4895e0001313677',
value: 'Home >> Services >> php '
},
{
label: '5e574a8ab4895e000131368d',
value: 'Home >> Services >> php >> Cake php '
},
{
label: '5e574ad1b4895e000131369c',
value: 'Home >> Services >> php >> Cake php >> version 7'
},
{
label: '5e574aa2b4895e0001313695',
value: 'Home >> Services >> php >> CodeIgniter'
},
{
label: '5e574a70b4895e000131367f',
value: 'Home >> Services >> React'
},
{
label: '5e574a7ab4895e0001313686',
value: 'Home >> Services >> Ruby on Rails'
},
{
label: '5e27f3ded2e5e91458745412',
value: 'Orphan'
},
{
label: '5e54a3dd7924a50001df0bd3',
value: 'Orphan >> New Page'
},
];
您可以为此使用简单的递归,每次函数调用时只需将名称累积到一个字符串中,如果存在则传递子项
const data = [{
"id": "5e27f3ded2e5e90001b4b989",
"title": "Home",
"children": [{
"id": "5e27f3dfd2e5e90001b4b993",
"title": "About Us",
"children": []
},
{
"id": "5e27f3dfd2e5e90001b4b996",
"title": "Contact Us",
"children": []
},
{
"id": "5e574a55b4895e0001313670",
"title": "Services",
"children": [{
"id": "5e574a63b4895e0001313677",
"title": "php",
"children": [{
"id": "5e574a8ab4895e000131368d",
"title": "Cake php",
"children": [{
"id": "5e574ad1b4895e000131369c",
"title": "version 7",
"children": []
}]
},
{
"id": "5e574aa2b4895e0001313695",
"title": "CodeIgniter",
"children": []
}
]
},
{
"id": "5e574a70b4895e000131367f",
"title": "React",
"children": []
},
{
"id": "5e574a7ab4895e0001313686",
"title": "Ruby on Rails",
"children": []
}
]
}
]
},
{
"id": "",
"title": "Orphan",
"children": [{
"id": "5e54a3dd7924a50001df0bd3",
"title": "New Page",
"children": []
}]
}
];
const array = [];
mapParent(data, '');
console.log(array);
function mapParent(data, name) {
data.forEach(d => {
array.push({
id: d.id,
label: name + d.title
});
if (d.children.length) {
mapParent(d.children, name + d.title + ' >> ')
}
})
};
I have an array of objects and want to get data from child to parent by their respected name like, if we have an array with multiple objects then I want:
- 主页
- 首页 >> 关于
- 主页 >> 博客
- 首页>>服务
- 首页 >> 服务 >> React
- 首页 >> 服务 >> PHP
- 首页 >> 服务 >> PHP >> 蛋糕
- 首页 >> 服务 >> PHP >> 蛋糕 >> V7
- 首页 >> 服务 >> PHP >> Laravel
- 首页 >> 服务 >> AIML
- 首页 >> 服务 >> ROR
- 首页 >> 联系我们
代码如下:
[{
"id": "5e27f3ded2e5e90001b4b989",
"title": "Home",
"children": [{
"id": "5e27f3dfd2e5e90001b4b993",
"title": "About Us",
"children": []
},
{
"id": "5e27f3dfd2e5e90001b4b996",
"title": "Contact Us",
"children": []
},
{
"id": "5e574a55b4895e0001313670",
"title": "Services",
"children": [{
"id": "5e574a63b4895e0001313677",
"title": "php",
"children": [{
"id": "5e574a8ab4895e000131368d",
"title": "Cake php",
"children": [{
"id": "5e574ad1b4895e000131369c",
"title": "version 7",
"children": []
}]
},
{
"id": "5e574aa2b4895e0001313695",
"title": "CodeIgniter",
"children": []
}
]
},
{
"id": "5e574a70b4895e000131367f",
"title": "React",
"children": []
},
{
"id": "5e574a7ab4895e0001313686",
"title": "Ruby on Rails",
"children": []
}
]
}
]
},
{
"id": "",
"title": "Orphan",
"children": [{
"id": "5e54a3dd7924a50001df0bd3",
"title": "New Page",
"children": []
}]
}
]
作为回应,我想要如下对象数组中的数据:
[{
label: '5e27f3ded2e5e90001b4b989',
value: 'Home'
},
{
label: '5e27f3dfd2e5e90001b4b993',
value: 'Home >> About Us"'
},
{
label: '5e27f3dfd2e5e90001b4b996',
value: 'Home >> Contact Us'
},
{
label: '5e574a55b4895e0001313670',
value: 'Home >> Services'
},
{
label: '5e574a63b4895e0001313677',
value: 'Home >> Services >> php '
},
{
label: '5e574a8ab4895e000131368d',
value: 'Home >> Services >> php >> Cake php '
},
{
label: '5e574ad1b4895e000131369c',
value: 'Home >> Services >> php >> Cake php >> version 7'
},
{
label: '5e574aa2b4895e0001313695',
value: 'Home >> Services >> php >> CodeIgniter'
},
{
label: '5e574a70b4895e000131367f',
value: 'Home >> Services >> React'
},
{
label: '5e574a7ab4895e0001313686',
value: 'Home >> Services >> Ruby on Rails'
},
{
label: '5e27f3ded2e5e91458745412',
value: 'Orphan'
},
{
label: '5e54a3dd7924a50001df0bd3',
value: 'Orphan >> New Page'
},
];
您可以为此使用简单的递归,每次函数调用时只需将名称累积到一个字符串中,如果存在则传递子项
const data = [{
"id": "5e27f3ded2e5e90001b4b989",
"title": "Home",
"children": [{
"id": "5e27f3dfd2e5e90001b4b993",
"title": "About Us",
"children": []
},
{
"id": "5e27f3dfd2e5e90001b4b996",
"title": "Contact Us",
"children": []
},
{
"id": "5e574a55b4895e0001313670",
"title": "Services",
"children": [{
"id": "5e574a63b4895e0001313677",
"title": "php",
"children": [{
"id": "5e574a8ab4895e000131368d",
"title": "Cake php",
"children": [{
"id": "5e574ad1b4895e000131369c",
"title": "version 7",
"children": []
}]
},
{
"id": "5e574aa2b4895e0001313695",
"title": "CodeIgniter",
"children": []
}
]
},
{
"id": "5e574a70b4895e000131367f",
"title": "React",
"children": []
},
{
"id": "5e574a7ab4895e0001313686",
"title": "Ruby on Rails",
"children": []
}
]
}
]
},
{
"id": "",
"title": "Orphan",
"children": [{
"id": "5e54a3dd7924a50001df0bd3",
"title": "New Page",
"children": []
}]
}
];
const array = [];
mapParent(data, '');
console.log(array);
function mapParent(data, name) {
data.forEach(d => {
array.push({
id: d.id,
label: name + d.title
});
if (d.children.length) {
mapParent(d.children, name + d.title + ' >> ')
}
})
};