Javascript 将对象数组中的对象转换为数组,同时保留其他数据
我有以下对象数组:Javascript 将对象数组中的对象转换为数组,同时保留其他数据,javascript,arrays,object,Javascript,Arrays,Object,我有以下对象数组: const datasubject = [ 0: { level: "standard 3" subject: "English" _id: xxx coreCompetencies { 0gHq0U5E667L4EdGbdZ2h: "Grammar", 9CfalSpzKYIV7AaWKBUwg: "Listening", 9boIfWUEGdj3WGxJL12XB: "Reading",
const datasubject =
[
0: {
level: "standard 3"
subject: "English"
_id: xxx
coreCompetencies {
0gHq0U5E667L4EdGbdZ2h: "Grammar",
9CfalSpzKYIV7AaWKBUwg: "Listening",
9boIfWUEGdj3WGxJL12XB: "Reading",
QZ11uYQ8CXkRk0LWenjqj: "Writing",
ZG1gtxRg6quIOYaTr6CUy: "Speaking"
}
},
1: {...},
2: {...}
]
我想将核心能力
更改为一个值数组,例如[“语法”、“听”、“读”、“写”、“说”]
,同时保留其他数据。我尝试了.map
,但由于它只返回对象,而没有其他细节,所以感到困惑。这就是我所做的,只返回core
对象:
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(value => {
value.coreCompetencies = Object.values(value.coreCompetencies);
return value;
})
非常感谢@tam dc!这是可行的,我使用您的解决方案是因为我可以看到“rest”在哪里。更好的实现:)谢谢@binariedme!我也尝试过你的解决方案,效果很好!超级棒!
const datacore = datasubject.map(({coreCompetencies, ...rest})=> {
return {...rest, coreCompetencies: Object.values(coreCompetencies)}
})