我试图使用for循环动态地向JSON对象数组添加新属性,但是如果我硬编码对象的索引而不是使用i,那就很奇怪了。
这有效:
sa.savannahWS.completedWorkOrder("09/29/2017").then(function(response) {
response.invocationResult.result[0].CSXPMTWO = "TEST";
response.invocationResult.result[1].CSXPMTWO = "TEST";
console.log(response.invocationResult.result);
}, function(error) {
sa.savannahUtils.showErrorPopup("Error In Creating Wiring House", error);
});
这些不起作用:
sa.savannahWS.completedWorkOrder("09/29/2017").then(function(response) {
console.log(response.invocationResult.result);
for(var i = 0; i<response.invocationResult.result.size; i++) {
response.invocationResult.result[i].CSXPMTWO = "TEST";
}
console.log(response.invocationResult.result);
}, function(error) {
sa.savannahUtils.showErrorPopup("Error In Creating Wiring House", error);
});
sa.savannahWS.completedWorkOrder("09/29/2017").then(function(response) {
console.log(response.invocationResult.result);
for(var i = 0; i<response.invocationResult.result.size; i++) {
response.invocationResult.result[i]["CSXPMTWO"] = "TEST";
}
console.log(response.invocationResult.result);
}, function(error) {
sa.savannahUtils.showErrorPopup("Error In Creating Wiring House", error);
});
sa.savannahWS.completedWorkOrder("09/29/2017").then(function(response) {
console.log(response.invocationResult.result);
for(var i = 0; i<response.invocationResult.result.size; i++) {
response.invocationResult.result[i].push({"CSXPMTWO": "TEST"});
}
console.log(response.invocationResult.result);
}, function(error) {
sa.savannahUtils.showErrorPopup("Error In Creating Wiring House", error);
});
答案 0 :(得分:0)
你确定是否定义了response.invocationResult.result.size?通常对于数组,数组的大小为.length