0

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 ?

10
  • 1
    responseQuestions.result.Questions[0].SelectedAnswerId = 4 etc ? Commented Feb 23, 2017 at 6:44
  • is it like you have selectedAnswerId in different array with ID of this JSON and you are trying to relate the things? Commented Feb 23, 2017 at 6:44
  • @user7417866. Yes I am trying the same Commented Feb 23, 2017 at 6:46
  • 1
    I think you are making it more complex than it needs to be. Why are you sending the entire json to the client and then back to server. Shouldn't you send back just the Selected Id? Commented Feb 23, 2017 at 6:55
  • 1
    @SRI see my answer below Commented Feb 23, 2017 at 7:09

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

exactly this what I mean into my answer.. didn't have time to create fiddle for the same.. but this should work.
Thanks for your valuable help.. This is working fine as expected.
0

You have to find index based on question id and then set answerid at specific index

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

Comments

0

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

second you get this array, you can loop through your first array and replace the string by compairing Id,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.