【发布时间】:2018-05-05 17:01:06
【问题描述】:
我有一个数组,其中包含可能具有第 n 级深度的对象。
类似这样的:
const settings = [
{path: '/templates/pictures.php', url: '/pictures', label: 'Pictures', component: 'tab', template: 'default'},
{path: '/templates/post-article.php', url: '/user/:username', component: 'table', template: 'default', children:[
{path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default', children:[
{path: '/templates/post-article-highlights.php', url: '/user/:username/highlights', component: 'table', template: 'default'}
]}
]}
]
我只需要在不同的数组上推送 'Url' 属性和 children 属性(如果存在),但要保留深度。
所以新数组应该是这样的:
const newArray = [
{url: '/pictures'},
{url: '/user/:username', children:[
{url: '/user/:username/highlights', children:[
{url: '/user/:username/highlights'}
]}
]}
]
你能帮帮我吗?
谢谢
【问题讨论】:
-
嗨!请使用tour 并通读help center,尤其是How do I ask a good question? 做你的研究,search 以获取有关 SO 的相关主题,然后试一试。 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您遇到的问题。人们会很乐意提供帮助。祝你好运!
标签: javascript arrays object recursion