简体   繁体   English

如何替换现有密钥的JSON数组中的值?

[英]how to replace the value in an JSON array for a existing key?

In my application I have a ajax call and am getting the following response based on this am constructing the questions & answer section which I have included in the JSFiddle 在我的应用程序中,我有一个ajax调用,并基于此正在构造以下已包含在JSFiddle中的 “问题与答案”部分,从而得到以下响应

var responseQuestions = {
  "error": false,
  "message": "Success",
  "result": {
    "Questions": [{
        "Id": "131a",
        "Text": "In what county do you live?",
        "Answers": [{
            "Id": "abc1",
            "Text": "option1"
          },
          {
            "Id": "abc2",
            "Text": "option2"
          },
          {
            "Id": "abc3",
            "Text": "option3"
          },
          {
            "Id": "abc4",
            "Text": "option4"
          },
          {
            "Id": "abc5",
            "Text": "option5"
          }
        ],
        "SelectedAnswerId": null
      },
      {
        "Id": "132a",
        "Text": "Which zip code has ever been a part of your address?",
        "Answers": [{
            "Id": "def1",
            "Text": "option1"
          },
          {
            "Id": "def2",
            "Text": "option2"
          },
          {
            "Id": "def3",
            "Text": "option3"
          },
          {
            "Id": "def4",
            "Text": "option4"
          },
          {
            "Id": "def5",
            "Text": "option5"
          }
        ],
        "SelectedAnswerId": null
      },
      {
        "Id": "133a",
        "Text": "What was the original amount of your most recent mortgage?",
        "Answers": [{
            "Id": "ghi1",
            "Text": "option1"
          },
          {
            "Id": "ghi2",
            "Text": "option2"
          },
          {
            "Id": "ghi3",
            "Text": "option3"
          },
          {
            "Id": "ghi4",
            "Text": "option4"
          },
          {
            "Id": "ghi5",
            "Text": "option5"
          }
        ],
        "SelectedAnswerId": null
      }
    ]
  }
};

Required Format: 所需格式:

 var responseQuestions = {
  "error": false,
  "message": "Success",
  "result": {
    "Questions": [{
        "Id": "131a",
        "Text": "In what county do you live?",
        "Answers": [{
            "Id": "abc1",
            "Text": "option1"
          },
          {
            "Id": "abc2",
            "Text": "option2"
          },
          {
            "Id": "abc3",
            "Text": "option3"
          },
          {
            "Id": "abc4",
            "Text": "option4"
          },
          {
            "Id": "abc5",
            "Text": "option5"
          }
        ],
        **"SelectedAnswerId": "abc2"**
      },
      {
        "Id": "132a",
        "Text": "Which zip code has ever been a part of your address?",
        "Answers": [{
            "Id": "def1",
            "Text": "option1"
          },
          {
            "Id": "def2",
            "Text": "option2"
          },
          {
            "Id": "def3",
            "Text": "option3"
          },
          {
            "Id": "def4",
            "Text": "option4"
          },
          {
            "Id": "def5",
            "Text": "option5"
          }
        ],
        **"SelectedAnswerId": "def1"**
      },
      {
        "Id": "133a",
        "Text": "What was the original amount of your most recent mortgage?",
        "Answers": [{
            "Id": "ghi1",
            "Text": "option1"
          },
          {
            "Id": "ghi2",
            "Text": "option2"
          },
          {
            "Id": "ghi3",
            "Text": "option3"
          },
          {
            "Id": "ghi4",
            "Text": "option4"
          },
          {
            "Id": "ghi5",
            "Text": "option5"
          }
        ],
        **"SelectedAnswerId": "ghi2"**
      }
    ]
  }
};

Now I need to submit this answer with the same above mentioned format along the " SelectedAnswerId " value(in the above mentioned array " SelectedAnswerId " is null and now I have to include the original selected ans Id based on questions ). 现在,我需要使用“ SelectedAnswerId ”值和上述相同的格式提交此答案(在上述数组“ SelectedAnswerId ”中为null,现在我必须根据问题包括原始选择的ans ID)。

I have tried to fetch all the selected ans id in a array and attached the same in jsfiddle but am unable to proceed on how to append this ans id in the existing array based on questions. 我试图获取数组中所有选定的ans id并将其附加到jsfiddle中,但基于问题无法继续如何将ans id附加到现有数组中。 How to achieve this ? 如何实现呢?

Try the following loops: 尝试以下循环:

      $('input[type="radio"]:checked').each(function() {
        var questionId = $(this).closest('.radioGroup').prev().attr('id');//get the question id
        var answerId = this.id;//get the answer id
        $.each(responseQuestions.result.Questions, function(i, v) {//loop each question

          if (v.Id == questionId) {
            $.each(v.Answers, function(ind, val) {//loop each answer
              if (val.Id == answerId) {
                responseQuestions.result.Questions[i]['SelectedAnswerId'] = answerId;//save the answer

              }
            });
          }
        });

      });


  console.log(responseQuestions);

demo: https://jsfiddle.net/mj3gvd5e/1/ 演示: https : //jsfiddle.net/mj3gvd5e/1/

Note: you need to change your question id to remove or add the a to be consistent both in the page and in the json 注意:您需要更改您的问题ID以删除或添加a,使其在页面和json中都保持一致

您必须根据问题ID查找索引,然后在特定索引处设置答案ID

var index = responseQuestions.result.Questions.findIndex(function(val){ return val.Id === yourQuestionid; }) responseQuestions.result.Questions[index].SelectedAnswerId = youranswerID;

First of all you need to have your main question id and Ans id into same array as given fiddle by Madalin jsfiddle.net/6q9mct68 首先,您需要将主要问题ID和Ans ID放入与Madalin jsfiddle.net/6q9mct68提琴一样的数组中

second you get this array, you can loop through your first array and replace the string by compairing Id, 第二个得到这个数组,就可以遍历第一个数组,并通过对Id进行替换来替换字符串,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM