如何解构对象中的数组并重命名从中创建的变量?
How to destructure an array within an object and rename the variables created from it?
我想解构以下对象(此处已简化):
export class Service = {
...
details: {
overview: [
{
title: {
de: 'Mock Example',
en: 'Mock Example',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
{
title: {
de: 'Mock Example 2',
en: 'Mock Example 2',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
],
...
我只想在右侧有“服务”,并将概览数组的索引 0 命名为“问题”,将概览数组的索引 1 命名为“解决方案”,如下所示:
const { problem, solution } = service;
我试过下面的方法,但是不行。而且我不太明白如何将变量重命名为“问题”和“解决方案”?
const {
details: {
overview[0]: {
...
},
},
details: {
overview[1]: {
...
}
}
} = service;
我猜,这就是你想要的:
const service = {
details: {
overview: [{
title: {
de: 'Mock Example',
en: 'Mock Example',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
{
title: {
de: 'Mock Example 2',
en: 'Mock Example 2',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
]
}
}
const {details: {overview: [problem, solution]}} = service
console.log(problem, solution)
我想解构以下对象(此处已简化):
export class Service = {
...
details: {
overview: [
{
title: {
de: 'Mock Example',
en: 'Mock Example',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
{
title: {
de: 'Mock Example 2',
en: 'Mock Example 2',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
],
...
我只想在右侧有“服务”,并将概览数组的索引 0 命名为“问题”,将概览数组的索引 1 命名为“解决方案”,如下所示:
const { problem, solution } = service;
我试过下面的方法,但是不行。而且我不太明白如何将变量重命名为“问题”和“解决方案”?
const {
details: {
overview[0]: {
...
},
},
details: {
overview[1]: {
...
}
}
} = service;
我猜,这就是你想要的:
const service = {
details: {
overview: [{
title: {
de: 'Mock Example',
en: 'Mock Example',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
{
title: {
de: 'Mock Example 2',
en: 'Mock Example 2',
},
description: {
de: 'Lorem ipsum...',
en: 'Lorem ipsum...',
},
},
]
}
}
const {details: {overview: [problem, solution]}} = service
console.log(problem, solution)