Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created March 2, 2016 15:30
Show Gist options
  • Save linxlad/153001184a5ce09297d8 to your computer and use it in GitHub Desktop.
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.
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