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

时间:2017-02-23 06:42:31

标签: javascript jquery arrays json

在我的应用程序中,我有一个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
      }
    ]
  }
};

必填格式:

 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"**
      }
    ]
  }
};

现在我需要在“ SelectedAnswerId ”值上提交相同的上述格式的答案(在上面提到的数组中“ SelectedAnswerId ”为空,现在我必须根据问题包括原始选定的ans Id。

我试图在数组中获取所有选定的ans id并在jsfiddle中附加相同但是无法继续如何根据问题在现有数组中附加此ans id。怎么做到这一点?

1 个答案:

答案 0 :(得分:0)

首先,您需要将主要问题ID和Ans id放入相同的数组中,如Madalin jsfiddle.net/6q9mct68

第二个你得到这个数组,你可以遍历你的第一个数组并通过compairing Id替换字符串,