javascript 基于父值在嵌套数组中添加新键值

mdfafbf1  于 2022-12-02  发布在  Java
关注(0)|答案(3)|浏览(230)

我有一个包含问题的数组。每个问题都有选项和正确答案。如果选项是正确的,我会尝试添加true/false。我应该如何最好地完成这项工作?我尝试了.map().forEach,但我不知道如何访问correct_answer,并使用它添加选项下的true/false值。

const data = [
    {
        "question": "What is the approximate value of mathematical constant e?",
        "options": [
            {
                "option": "1.41",
            },
            {
                "option": "1.62",
            },
            {
                "option": "2.72",
            },
            {
                "option": "3.14",
            }
        ],
        "correct_answer": "2.72"
    },
    {
        "question": "What is the name of the main character of the anime "One-Punch Man"?",
        "options": [
            {
                "option": "Genos",
            },
            {
                "option": "King",
            },
            {
                "option": "Saitama",
            },
            {
                "option": "Sonic",
            }
        ],
        "correct_answer": "Saitama"
    }
]

预期结果

const data = [
    {
        "question": "What is the approximate value of mathematical constant e?",
        "options": [
            {
                "option": "1.41",
                "correct_answer": false,
            },
            {
                "option": "1.62",
                "correct_answer": false,
            },
            {
                "option": "2.72",
                "correct_answer": true,
            },
            {
                "option": "3.14",
                "correct_answer": false,
            }
        ],
        "correct_answer": "2.72"
    },
    {
        "question": "What is the name of the main character of the anime "One-Punch Man"?",
        "options": [
            {
                "option": "Genos",
                "correct_answer": false,
            },
            {
                "option": "King",
                "correct_answer": false,
            },
            {
                "option": "Saitama",
                "correct_answer": true,
            },
            {
                "option": "Sonic",
                "correct_answer": false,
            }
        ],
        "correct_answer": "Saitama"
    }
]
wfveoks0

wfveoks01#

您可以使用Array.map更新值,并使用spread运算子填入对象。

let newdata = data.map(d => ({ ...d,
  options: d.options.map(m => ({ ...m,
    correct_answer: m.option == d.correct_answer ? true : false
  }))
}))
const data = [{
    "question": "What is the approximate value of mathematical constant e?",
    "options": [{
        "option": "1.41",
      },
      {
        "option": "1.62",
      },
      {
        "option": "2.72",
      },
      {
        "option": "3.14",
      }
    ],
    "correct_answer": "2.72"
  },
  {
    "question": "What is the name of the main character of the anime "One-Punch Man"?",
    "options": [{
        "option": "Genos",
      },
      {
        "option": "King",
      },
      {
        "option": "Saitama",
      },
      {
        "option": "Sonic",
      }
    ],
    "correct_answer": "Saitama"
  }
]

let newdata = data.map(d => ({ ...d,
  options: d.options.map(m => ({ ...m,
    correct_answer: m.option == d.correct_answer ? true : false
  }))
}))

console.log(newdata)
uhry853o

uhry853o2#

const data = [{
    "question": "What is the approximate value of mathematical constant e?",
    "options": [{
        "option": "1.41",
      },
      {
        "option": "1.62",
      },
      {
        "option": "2.72",
      },
      {
        "option": "3.14",
      }
    ],
    "correct_answer": "2.72"
  },
  {
    "question": "What is the name of the main character of the anime "One-Punch Man"?",
    "options": [{
        "option": "Genos",
      },
      {
        "option": "King",
      },
      {
        "option": "Saitama",
      },
      {
        "option": "Sonic",
      }
    ],
    "correct_answer": "Saitama"
  }
]

data.forEach(question => {
  question.options.forEach(option => {
    option.correct_answer = option.option === question.correct_answer
  })
})

console.log(data)
bf1o4zei

bf1o4zei3#

若要取得想要的结果,您可以使用map()方法来重复数据数组,以及使用forEach()方法来重复选项数组。在传递给forEach()的回呼函数中,您可以使用if陈述式来检查目前的选项是否相等正确的答案,并将correct_answer属性设定为true或false。
下面是一个如何实现这一点的示例:

const data = [
    {
        "question": "What is the approximate value of mathematical constant e?",
        "options": [
            {
                "option": "1.41",
            },
            {
                "option": "1.62",
            },
            {
                "option": "2.72",
            },
            {
                "option": "3.14",
            }
        ],
        "correct_answer": "2.72"
    },
    {
        "question": "What is the name of the main character of the anime "One-Punch Man"?",
        "options": [
            {
                "option": "Genos",
            },
            {
                "option": "King",
            },
            {
                "option": "Saitama",
            },
            {
                "option": "Sonic",
            }
        ],
        "correct_answer": "Saitama"
    }
];

const updatedData = data.map(datum => {
  const options = datum.options.map(option => {
    if (option.option === datum.correct_answer) {
      return {...option, correct_answer: true};
    } else {
      return {...option, correct_answer: false};
    }
  });

  return {...datum, options};
});

console.log(updatedData);

在上面的代码中,我们首先使用map()迭代数据数组。对于数据数组中的每个对象,我们再次使用map()方法迭代options数组。在传递给map()的回调函数中,我们检查当前选项的option属性是否等于当前数据的correct_answer属性。如果它们相等,我们返回一个与当前选项具有相同属性的新对象,加上一个设置为true的新属性correct_answer。如果它们不相等,我们返回一个与当前选项具有相同属性的新对象,加上一个设置为false的新属性correct_answer。
在内部map()调用完成后,我们返回一个新对象,该对象具有与当前数据相同的属性,加上一个设置为更新的选项数组的新属性选项。
最后,我们将更新后的数据数组输出到控制台。您可以修改此代码以满足您的特定需要。

相关问题