Wanting to loop through and create an object that contains key => [value]
, if the key already exists, simply add the value.
Here is what I have tried so far:
let dataName = [];
let obj = {};
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
obj[name] = [checkbox.id]
dataName.push(obj);
}
});
However, each time I click, the value overrides instead of being added. How can I make sure values are added by keys when a checkbox is checked?