簡體   English   中英

使用forloop將新屬性添加到JSON不起作用

[英]Adding new properties to JSON with a forloop doesn't work

我正在嘗試使用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);
                });

您確定已定義response.invocationResult.result.size嗎? 通常對於數組,數組的大小為.length

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM