使用数组更改数组中的所有键也更深的对象内部
Change all keys in array also deeper inside object with array
目前我们的导航结构有以下数组:
{
childNodes: [
{title: "Mijn afdeling", type: "Department", relativeUri: "/mijnafdeling"},
{title: "contact", type: "Department", relativeUri: "/contact"}
]
RelativeUri: '/',
title: 'test'
},
{
childNodes: null
RelativeUri: '/',
title: 'test'
}
]
现在我想将所有 'childNodes' 键更改为 'child' 并将所有 relativeUri 键更改为 href。
我想要的结果应该是
{
child: [
{title: "Mijn afdeling", type: "Department", href: "/mijnafdeling"},
{title: "contact", type: "Department", href: "/contact"}
]
href: '/',
title: 'test'
},
{
child: null
href: '/',
title: 'test'
}
]
我知道我可以做类似的事情 this.navigationArray = this.navigationArray.map(({ childNodes, relativeUri }) => ({ childNodes: child, href: relativeUri }))
但是我也不知道如何获取 childNodes 对象下数组中的所有键。所以它基本上需要 'find' 或遍历数组内的所有对象,并找到特定的键。 'childNodes' 对象也可以为空。
对于不同的页面,我有不同数量的导航项,所以我宁愿不针对特定的索引,因为这会弄乱导航。
非常感谢!
应该这样做:
const nu = orig
.map(
({
childNodes,
RelativeUri,
title
}) => ({
child: childNodes ?
childNodes.map(({
title,
type,
relativeUri
}) => ({
title,
type,
href: relativeUri
})) : childNodes,
href: RelativeUri,
title
}));
console.log(nu);
演示版
const orig = [
{
childNodes: [{
title: "Mijn afdeling",
type: "Department",
relativeUri: "/mijnafdeling"
},
{
title: "contact",
type: "Department",
relativeUri: "/contact"
}
],
RelativeUri: '/',
title: 'test'
},
{
childNodes: null,
RelativeUri: '/',
title: 'test'
}
];
//console.log( orig );
const nu = orig
.map(({childNodes,RelativeUri,title}) => ({child: childNodes ? childNodes.map(({title,type,relativeUri}) => ({title,type,href:relativeUri})) : childNodes, href:RelativeUri, title}));
console.log( nu );
目前我们的导航结构有以下数组:
{
childNodes: [
{title: "Mijn afdeling", type: "Department", relativeUri: "/mijnafdeling"},
{title: "contact", type: "Department", relativeUri: "/contact"}
]
RelativeUri: '/',
title: 'test'
},
{
childNodes: null
RelativeUri: '/',
title: 'test'
}
]
现在我想将所有 'childNodes' 键更改为 'child' 并将所有 relativeUri 键更改为 href。
我想要的结果应该是
{
child: [
{title: "Mijn afdeling", type: "Department", href: "/mijnafdeling"},
{title: "contact", type: "Department", href: "/contact"}
]
href: '/',
title: 'test'
},
{
child: null
href: '/',
title: 'test'
}
]
我知道我可以做类似的事情 this.navigationArray = this.navigationArray.map(({ childNodes, relativeUri }) => ({ childNodes: child, href: relativeUri }))
但是我也不知道如何获取 childNodes 对象下数组中的所有键。所以它基本上需要 'find' 或遍历数组内的所有对象,并找到特定的键。 'childNodes' 对象也可以为空。
对于不同的页面,我有不同数量的导航项,所以我宁愿不针对特定的索引,因为这会弄乱导航。
非常感谢!
应该这样做:
const nu = orig
.map(
({
childNodes,
RelativeUri,
title
}) => ({
child: childNodes ?
childNodes.map(({
title,
type,
relativeUri
}) => ({
title,
type,
href: relativeUri
})) : childNodes,
href: RelativeUri,
title
}));
console.log(nu);
演示版
const orig = [
{
childNodes: [{
title: "Mijn afdeling",
type: "Department",
relativeUri: "/mijnafdeling"
},
{
title: "contact",
type: "Department",
relativeUri: "/contact"
}
],
RelativeUri: '/',
title: 'test'
},
{
childNodes: null,
RelativeUri: '/',
title: 'test'
}
];
//console.log( orig );
const nu = orig
.map(({childNodes,RelativeUri,title}) => ({child: childNodes ? childNodes.map(({title,type,relativeUri}) => ({title,type,href:relativeUri})) : childNodes, href:RelativeUri, title}));
console.log( nu );