gpt4 book ai didi

javascript - 从现有数组创建一个新数组

转载 作者:行者123 更新时间:2023-11-28 11:30:24 25 4
gpt4 key购买 nike

我有一个像这样的现有数组

"activeSubject": [
{
"subject_id": "Class Teacher",
"subject_name": "Class Teacher",
"status": "2"
},
{
"subject_id": "Academic Leader (Head)",
"subject_name": "Academic Leader (Head)",
"status": "2"
},
{
"subject_id": "15",
"subject_name": "Physics",
"status": "2"
}
]

然后我想创建一个新数组,如下所示

"new_subjects": [
{
"value": "Class Teacher",
},
{
"value": "Academic Leader (Head)",
},
{
"value": "Physics",
}
]

我已经通过map()尝试过这个方法

var objA = this.state.activeSubject.map(o => ({
[o]: { value: o.subject_name}
}))

但它没有给出正确的数组,请帮我解决这个问题

最佳答案

你的map语法很奇怪,我不知道你想做什么,但这应该足够了;)

this.state = {"activeSubject": [
{
"subject_id": "Class Teacher",
"subject_name": "Class Teacher",
"status": "2"
},
{
"subject_id": "Academic Leader (Head)",
"subject_name": "Academic Leader (Head)",
"status": "2"
},
{
"subject_id": "15",
"subject_name": "Physics",
"status": "2"
}
]};

var objA = this.state.activeSubject.map(o => ({ value: o.subject_name}));
console.log(objA);

关于javascript - 从现有数组创建一个新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51279509/

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