How about this:
var allButFirstThreeAreNull = true; for (var i = 4; i <= 50; i++) { if (myObject['a' + i] !== null) { allButFirstThreeAreNull = false; break; } }
The key point here is the ability to access a property using some complex expression when using parenthesis notation ( object[property_expression] ). And, of course, you do not need to check other properties if you find one that is not equal to zero; hence the use of break .
Now it turns out that these properties are dynamic. Well, there is one thing to do this:
var i, l, isValid = true, props = Object.keys(myObject); for (i = 3, l = props.length; i < l; i++) { if (myObject[ props[i] ] !== null) { isValid = false; break; } }
... except that your first three properties may not actually be the ones you think of. Check this out, for example:
var foo = { '12': null, booya: 3 '2': null, '3': null, };
Writing Object.keys will give you ["2", "3", "12", "booya"] , putting the numeric properties in front (and sorting them by number).
source share