Reputation: 205
in order to find an "intersection" of multiple users' settings (boolean), I need to compare their setting objects with each other. For example: if you click on a user, you see his settings and can edit them but if you select multiple users which can have different settings, I want to be able to show an indeterminate status in order to display that a specific setting is set to true for some users and to false for others. In case of multi select, the same settings will be applied to all selected users.
Here is an example of what I came up with so far and would like to know if there is a more elegant way to do it (as soon as there is at least two users with different settings, then I can ignore the rest): http://jsfiddle.net/kT7UP/
Thanks in advance.
Upvotes: 0
Views: 377
Reputation: 707376
You can make each property test into a common function where you pass in the property name. So all your repetitive for
loops become this:
function checkCommonValue(prop) {
for (var j = 0; j < userAll.length; j++) {
if (userAll[j][prop] != model[prop]) {
console.log('Indeterminate status');
return(false);
}
}
return(true);
}
checkCommonValue("Printing");
checkCommonValue("Sharing");
checkCommonValue("Reading");
checkCommonValue("AccountEnabled");
checkCommonValue("Fax");
Keep in mind the basic coding principle DRY (don't repeat yourself). If you find you're repeating code blocks that are almost the same, then it's time to think of a way to break that code down into common functions that you can call from multiple places rather than copy/paste the same code into multiple places.
Upvotes: 2