【问题标题】:How can i apply map function to update all element of JSON body我如何应用地图功能来更新 JSON 正文的所有元素
【发布时间】:2017-12-21 11:12:08
【问题描述】:
async editSurvey(answers) {
 let answer_text;
 let id;
 answers.map((answer, index) => {
   answer_text = answer.answer_text;
   id = answer.id;
});

body: JSON.stringify({
    question: {
      answers_attributes: {
        '0': {
          answer_text: answer_text,
          id: id
        } }
    }
  })
}

这是 api 代码,我想要 map 函数,以便在每张地图上它都应该转到 JSON.stringify 并分配值,但在我的情况下,它会转到 JSON.stringify 作为最后一个地图元素。所以请建议我任何解决方案。 提前谢谢你。

【问题讨论】:

    标签: json reactjs api react-native


    【解决方案1】:
    async editSurvey(answers) {
     let answer_text;
     let id;
     answers.map((answer, index) => {
       answer_text = answer.answer_text;
       id = answer.id;
       return {
         body: JSON.stringify({
        question: {
          answers_attributes: {
            '0': {
              answer_text: answer_text,
              id: id
            }
         }
        }
       }
    });
    

    尝试做这样的事情,检查一次括号

    【讨论】:

    • 感谢您的回复。它的工作,但需要刷新页面。最初它给出的错误是“ExceptionsManager.js:71 Unhandled promise reject TypeError: Cannot read property 'json' of undefined”。请提出解决方案。
    • POST 方法如何工作,因为它为 answers.length 时间创建完整数据,我的意思是整个数据进入一个循环。
    猜你喜欢
    • 2022-01-20
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2019-02-21
    • 2020-01-24
    相关资源
    最近更新 更多