[英]how to convert plain array into nested object which is nested into parent object
I'm trying to convert an plain array into object with additional parameter我正在尝试将普通数组转换为带有附加参数的对象
Lets say, I have an object可以说,我有一个对象
var obj = [{
index: 1,
question: "If a carrier wave of 1000 kHz is used to carry the signal, the length of transmitting antenna will be equal to _____ ?",
choices_list: ["300 m", "30 m", "3 m", "0.3 m"],
answer: "300 m",
explanation: "h = c/v = 3 × 10^8/10^6 = 300 m",
},{...}
{...}
{...}
...
...]
now With the help of below code I'll convert choices_list
in to object现在在下面代码的帮助下,我将把choices_list
转换为对象
res_quiz_data.forEach(key => {
console.log(key.choices_list);
const map1 = key.choices_list.map(x => {
return { selected: false, choice: x }
});
console.log(map1);
})
Output输出
JS: [300 m, 30 m, 3 m, 0.3 m]
JS: [{
JS: "selected": false,
JS: "choice": "300 m"
JS: }, {
JS: "selected": false,
JS: "choice": "30 m"
JS: }, {
JS: "selected": false,
JS: "choice": "3 m"
JS: }, {
JS: "selected": false,
JS: "choice": "0.3 m"
JS: }]
now i need to way to modify the main Json object现在我需要修改主要的 Json 对象
Expected Output预期产出
var obj = [{
index: 1,
question: "If a carrier wave of 1000 kHz is used to carry the signal, the length of transmitting antenna will be equal to _____ ?",
choices_list: [{
"selected": false,
"choice": "300 m"
}, {
"selected": false,
"choice": "30 m"
}, {
"selected": false,
"choice": "3 m"
}, {
"selected": false,
"choice": "0.3 m"
}],
answer: "300 m",
explanation: "h = c/v = 3 × 10^8/10^6 = 300 m",
},
{....},
{....},
{....},
....
....]
You can update as follows您可以按以下方式更新
var obj = [{ index: 1, question: "If a carrier wave of 1000 kHz is used to carry the signal, the length of transmitting antenna will be equal to _____ ?", choices_list: ["300 m", "30 m", "3 m", "0.3 m"], answer: "300 m", explanation: "h = c/v = 3 × 10^8/10^6 = 300 m", }] var output = obj.map(o => { o.choices_list = o.choices_list.map(choice => ({selected: false, choice})) return o }) console.log(output)
If you want to update on the same object, you can do as follows如果你想在同一个对象上更新,你可以这样做
var _obj = [{ index: 1, question: "If a carrier wave of 1000 kHz is used to carry the signal, the length of transmitting antenna will be equal to _____ ?", choices_list: ["300 m", "30 m", "3 m", "0.3 m"], answer: "300 m", explanation: "h = c/v = 3 × 10^8/10^6 = 300 m", }] _obj.forEach(o => { o.choices_list = o.choices_list.map(choice => ({selected: false, choice})) return o }) console.log(_obj)
Another option to make the logic simpler, you can destructure object to operate separately on choices_list
and spread syntax to build up the result without caring about the unchanged fields.另一个使逻辑更简单的选项,您可以解构 object以单独对choices_list
进行操作,并传播语法以构建结果,而无需关心未更改的字段。
const myArray = [{ index: 1, question: "If a carrier wave of 1000 kHz is used to carry the signal, the length of transmitting antenna will be equal to _____ ?", choices_list: ["300 m", "30 m", "3 m", "0.3 m"], answer: "300 m", explanation: "h = c/v = 3 × 10^8/10^6 = 300 m" }, { index: 2, question: "Dummy question", choices_list: ["a", "b", "c", "d"], answer: "d", explanation: "none" }] const res = myArray.map(({choices_list, ...a}) => ({ ...a, choices_list: choices_list.map(x => ({selected: false, choice: x})) })) console.log(res)
Note that th solution does not mutate the original array, which is generally desirable.请注意,该解决方案不会改变原始数组,这通常是可取的。
Isn't是不是
obj.forEach((entry) {
res_quiz_data.forEach(key => {
console.log(key.choices_list);
const map = key.choices_list.map(x => {
return { selected: false, choice: x }
});
console.log(map);
});
entry.choices_list = map;
});
Good enough?够好了?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.