我的應用正在打擊返回一些JSON記錄的WebAPI。我通過jQuery AJAX獲取它們,並將JSON分配給一個JavaScript變量。我可以循環並對現有項目進行更改,而不會出現問題。但是,如何爲此對象添加更多「記錄」?我試圖理解結果變量的結構。添加項目到Javascript對象
這是我作爲一個測試。這是最好的方法嗎?
var trustAccounts = {"accounts":[
{"entityId":12345,
"type":"IOLTA",
"nameOnAccount":"Sam Smith Trust",
"accountNumber":"987654",
"bankCode":"003",
"bankName":"Bank of Stuff",
"accountDate":"12/15/2014",
"status":"A",
"exempt":"N",
"accountId":142922,
"action":"U"}]};
var newaccount = {};
newaccount.entityId = 23456;
newaccount.type = "IOLTA";
newaccount.nameOnAccount = "John Smith Trust";
newaccount.accountNumber = "789456";
newaccount.bankCode = "003";
newaccount.bankName = "Bank of Stuff";
newaccount.accountDate = "12/15/2014";
newaccount.status = "A";
newaccount.exempt = "N";
newaccount.accountId = 142923;
newaccount.action = "U";
trustAccounts.accounts.push(newaccount);
console.log(trustAccounts);
請問您能否發佈相關代碼,以便我們有一個共同的討論基礎?謝謝 ! – Christos
我假設JSON是一個對象數組?在這種情況下,您需要創建一個新對象,然後使用'records.push(newRecord);' –
*「我試圖理解結果變量的結構來添加它。」*:並且您認爲沒有看到您的JSON,...我們可以嗎? – trincot