如何销毁对象中的数组并重命名从中创建的变量



我想销毁以下对象(此处简化(:

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命名为"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)

最新更新