I have a dynamic form element that I want to save using ajax. I formatted the data as a javascript object, and am trying to update each of the elements. But I'm having trouble accessing a nested element so I can set a new value. The result from below is 'undefined' but I want to do is look up an item from they array by number, so I can update the qty value.
// 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));