Skip to content

Instantly share code, notes, and snippets.

@vonKristoff
Last active August 29, 2015 14:05
Show Gist options
  • Save vonKristoff/2b6bfb80d17f2a778450 to your computer and use it in GitHub Desktop.
Save vonKristoff/2b6bfb80d17f2a778450 to your computer and use it in GitHub Desktop.
Toggle the Object keys boolean values, setting all to false, but the key you want to set as true.
function truthify (scope, child) {
var obj = scope;
// set param to true on selectee
obj[child] = true;
for (var el in obj) {
// if obj is set to true && not equal to who
if(obj[el] && el != child){
// reverse bool
obj[el] = !obj[el];
}
}
// console.log(obj); // see output change
};
// Usage Example
var obj = {
"level" : {
"ace" : true,
"base": false,
"case": false
}
};
// set 'base' to true, and rest as false
truthify(obj.level, 'base');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment