When selecting objects from a list using a checkbox, I want to make the selection recognizable by adding a new key-value pair to the selected objects with the label of the selected value.
This is what I have tried:
var checkCheckboxOnOff = [{"fieldName":"Category","picklistValue":"Technology"},{"fieldName":"Region","picklistValue":"Central"}]
Here is how I loop through checkCheckboxOnOff to select objects from a list:
filteredList = serverList.filter(function(item) { return item[checkCheckboxOnOff[i].fieldName] == checkCheckboxOnOff[i].picklistValue});
While looping through the list, I add a key and value to the selected objects and push them into a filter array:
filteredList.forEach(function(n) {n.arrayPicklist = checkCheckboxOnOff.fieldName});
Array.prototype.push.apply(filterArray,filteredList[i]);
The issue I am facing is that when I select an object twice, both selected objects end up being updated with the last key and value. I expect each selected object to have a unique value even if selected multiple times.
For example:
[{"Id":"120"},{"Id":"121"}]
2nd Selection
[{"Id":"123"},{"Id":"121"}]
Expected outcome:
[{"Id":"120","arrayPicklist":"Category"},
{"Id":"121","arrayPicklist":"Category"},{"Id":"123","arrayPicklist":"Region"},
{"Id":"121","arrayPicklist":"Region"}]
Current result
[{"Id":"120","arrayPicklist":"Category"},
{"Id":"121","arrayPicklist":"Region"},{"Id":"123","arrayPicklist":"Region"},
{"Id":"121","arrayPicklist":"Region"}]
There seems to be an issue with getting undefined for the value, please refer to this fiddle: https://jsfiddle.net/Lcg1qca3/18/