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

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 ).

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. How to achieve this ?

Answers 1

  • 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/

    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


Related Articles