我有一個動態表單元素,我想使用ajax保存。我將數據格式化爲javascript對象,並試圖更新每個元素。但是我無法訪問嵌套元素,所以我可以設置一個新的值。下面的結果是'未定義',但我想要做的是從數組中按數字查找一個項目,以便我可以更新數量值。訪問嵌套javascript對象
// object to hold form data for ajax submit
var jsonLoad = {
type: 'estimate',
note: null,
terms: null,
tax: 0,
items: [
{
1 :
{
description: 'test123',
rate: 300,
qty: 1
}
},
{
2 :
{
description: 'test555',
rate: 600,
qty: 2
}
}
]
};
function getItem(i) {
jsonLoad.items.forEach(function (element) {
if (element[i] === i) {
return(element[i]);
}
});
}
console.log(getItem(2));