gpt4 book ai didi

javascript - 将对象数组中的对象转换为数组,同时保留其他数据

转载 作者:行者123 更新时间:2023-11-28 12:13:35 26 4
gpt4 key购买 nike

我有以下对象数组:

const datasubject = 
[
0: {
level: "standard 3"
subject: "English"
_id: xxx
coreCompetencies {
0gHq0U5E667L4EdGbdZ2h: "Grammar",
9CfalSpzKYIV7AaWKBUwg: "Listening",
9boIfWUEGdj3WGxJL12XB: "Reading",
QZ11uYQ8CXkRk0LWenjqj: "Writing",
ZG1gtxRg6quIOYaTr6CUy: "Speaking"
}
},
1: {...},
2: {...}
]

我想将 coreCompetcies 更改为值数组,例如[“语法”、“听力”、“阅读”、“写作”、“口语”],同时保留其他数据。我尝试过 .map 但感到困惑,因为它只返回 coreCompetcies 对象,而没有其他详细信息。这就是我所做的,仅返回 coreCompetcies 对象:

const datacore = datasubject.map(value => value.coreCompetencies);

我想实现这样的目标:

const datasubject = 
[
0: {
level: "standard 3"
subject: "English"
_id: xxx
coreCompetencies ["Grammar","Listening","Reading","Writing","Speaking"]
},
1: {...},
2: {...}
]

最佳答案

const datacore = datasubject.map(({coreCompetencies, ...rest})=> {
return {...rest, coreCompetencies: Object.values(coreCompetencies)}
})

关于javascript - 将对象数组中的对象转换为数组,同时保留其他数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55035178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com