如何在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"
}
]
}
答案 0 :(得分:0)
您可以从现有的
创建新对象
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)
&#13;