Your idea should work fine:
var i; var index = 0; var j = 0; var chkgroup = ['timing', 'concern', 'steps']; var currentGroup; for (j = 0; j < chkgroup.length; ++j) { currentGroup = chkgroup[j]; i = masterdata[currentGroup].split(','); for (index = 0; index < i.length; ++index) { $("#" + currentGroup + "_" + i[index].trim()) .prop('checked', true); } }
If the chkgroup array really matches the keys object in masterdata , you can use the external for..in loop for..in :
var i; var index = 0; var currentGroup; for (currentGroup in masterdata) { i = masterdata[currentGroup].split(','); for (index = 0; index < i.length; ++index) { $("#" + currentGroup + "_" + i[index].trim()) .prop('checked', true); } }
Note that there is no order for for...in , so if you need to ensure that you iterate over the properties of an object in a specific order, it might be better to use a predefined array.
You can also get an idea of $.map :
var values = $.map(masterdata, function (i, currentGroup) { return $.map(i.split(','), function (val) { return $('#' + currentGroup + '_' + val.trim()); }); }); $(values).prop('checked', true);
source share