2017-10-03 11 views
0

forループを使用して動的にJSONオブジェクトの配列に新しいプロパティを追加しようとしていますが、オブジェクトのインデックスをハードコードすると私はそれを使用して動作します。forループでJSONに新しいプロパティを追加できない

これは動作します:

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); 
       }); 
+1

です。それは' .result.size'ではなく '.result.length'で終わるべきです。 –

+0

あなたは正しいです。私は愚かな間違いをした。 ".length" worked –

答えて

0

はよろしいresponse.invocationResult.result.size私です定義されていますか?通常、配列の場合、 `response.invocationResult.result`が配列の場合、配列のサイズは.length

+0

あなたは100%正しいです。長さは働いた。私はなぜ 'sizeが私にコンソールにエラーを与えなかったのか分からない –

関連する問題