Last active
August 29, 2015 14:05
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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