2017-10-03 1 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가 나에게 콘솔에서 에러를주지 않았는 지 안다. –

관련 문제