Created
March 2, 2016 15:30
-
-
Save linxlad/153001184a5ce09297d8 to your computer and use it in GitHub Desktop.
Get the size of an object and determine if the object has any true values.
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
Object.prototype.anyValueTrue = function() { | |
var key; | |
for (var key in this) { | |
if (this.hasOwnProperty(key)) { | |
if (this[key] === true) { | |
return true; | |
} | |
} | |
} | |
return false; | |
}; | |
Object.prototype.size = function() { | |
var size = 0, key; | |
for (key in this) { | |
if (this.hasOwnProperty(key)) { | |
size++; | |
} | |
} | |
return size; | |
}; | |
var myObject = { | |
'valueOne': false, | |
'valueTwo': true, | |
'valueThree': false, | |
'valueFour': false | |
}; | |
alert(communicationAndUnderstanding.size()); | |
alert(communicationAndUnderstanding.anyValueTrue()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment