簡體   English   中英

如何將普通數組轉換為嵌套在父對象中的嵌套對象

[英]how to convert plain array into nested object which is nested into parent 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",
},{...}
{...}
{...}
...
...]

現在在下面代碼的幫助下,我將把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);
})

輸出

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: }]

現在我需要修改主要的 Json 對象

預期產出

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",
    },
{....},
{....},
{....},
....
....]

您可以按以下方式更新

 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)

如果你想在同一個對象上更新,你可以這樣做

 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)

另一個使邏輯更簡單的選項,您可以解構 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)

請注意,該解決方案不會改變原始數組,這通常是可取的。

是不是

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;
});

夠好了?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM