I'm trying to loop through input fields with an each loop, add other form data to an array and then add that input value to the form data already in the array.
It seems to be basically working but every time I set the input it sets that value in all the entries with that name in the array. Any ideas about where my logic is going sideways?
$(this).next('tr').children('td').each(function () {
input = $(this).children('input');
units[i] = unit;
units[i]['barcode'] = input.val();
i++;
});
Output object: Object {0: Object, 1: Object} 0: Object barcode: "789" qty: "3" size: 15 status: "received"
1: Object barcode: "789" qty: "3" size: 15 status: "received"
The input.val() is different on each iteration of the each loop, it just assigns the last value to all barcode entries.
unit
andi
? – Shomz Jan 14 at 0:28unit
is an object defined before your loop, you're assigning every array element to reference the same object. You need to add some code to create a new object on each iteration. – nnnnnn Jan 14 at 0:31