繁体   English   中英

如何在angularjs控制器中将JSON数组更新为列表对象

[英]How to update JSON array into list object in angularjs controller

如何在angularjs控制器中用新对象修改JSON对象。 我给出了两种json格式。 我需要使用新的JSON对象修改JSON。

输入来自表单提交

{
  "motivationForFinDifficulty": "sds",
  "motivationForConcession": "sddsd",
  "motivationForModificationOrRefin": "dsasa",
  "administrationVO": {
    "measureCode": {
      "0": "C",
      "1": "B"
    },
    "measureType": {
      "0": "Refinancing",
      "1": "Temporary"
    },
    "measureDescription": {
      "0": "ok",
      "1": "ok"
    }
  }
}



<pre>

I want this below output .



{
  "motivationForFinDifficulty": "sds",
  "motivationForConcession": "sddsd",
  "motivationForModificationOrRefin": "dsasa",
  "administrationVO": [{
    "measureCode": "C",
    "measureType": "Refinancing",
    "measureDescription": "ok"
  }
  {
    "measureCode": "B",
    "measureType": "Temporary",
    "measureDescription": "ok"
  }
]

}

您可以从现有对象创建一个新对象

 var x = { "motivationForFinDifficulty": "sds", "motivationForConcession": "sddsd", "motivationForModificationOrRefin": "dsasa", "administrationVO": { "measureCode": { "0": "C", "1": "B" }, "measureType": { "0": "Refinancing", "1": "Temporary" }, "measureDescription": { "0": "ok", "1": "ok" } } } // getting getAdministrationVO var getAdministrationVO = x.administrationVO; // an empty array for pushing objects var administrationVO = []; // get all the keys from the administrationVO var keysLength = Object.keys(getAdministrationVO); // considering all keys(object) of administrationVO have same number of keys var subKeys = Object.keys(getAdministrationVO.measureCode); // loop through it and add value from each of the nested object for (var i = 0; i < subKeys.length; i++) { var m = {}; m.measureCode = getAdministrationVO.measureCode[subKeys[i]]; m.measureType = getAdministrationVO.measureType[subKeys[i]]; m.measureDescription = getAdministrationVO.measureDescription[subKeys[i]]; // push the object in array administrationVO.push(m) } // create new object using predefined value and newly created array of object of administrationVO var newObj = { "motivationForFinDifficulty": "sds", "motivationForConcession": "sddsd", "motivationForModificationOrRefin": "dsasa", "administrationVO": administrationVO }; console.log(newObj) 

暂无
暂无

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

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