When a user clicks "save" I need to collect all the data from inputs with a class of input_product and compile it to data with this format:
data = {param1:[{upc:'upc', value:'value'}, ... ], ... paramN: [ ... ]}
I tried doing this with the code below, but I keep getting this error:
Uncaught TypeError: Cannot read property 'push' of undefined
$('.save').on('click', function() {
event.preventDefault();
var data = {};
$('.input_product').each(function() {
const
param = $(this).attr('name'),
upc = $(this).parent().parent().attr('id'),
value = $(this).val()
console.log(param, upc, value); //prints qty, 1001, 5
if (value && param) {
if (!param in data) {
data[param] = [];
}
data[param].push({'upc':upc, 'value':value}); // Error is thrown here
}
});
window.someFunction(data);
});
What am I missing here?
dataandparam?