How can I get the object length ?
In console my object looks like this:
Object {
2: true,
3: true,
4: true
}
.length
w
You don't want to repeat that Object.keys(obj) everywhere you want to get the length, as the code might get pretty messy. Therefore just put it in some personal project snippet library to be available across the site:
Object.prototype.length = function(){
return Object.keys(this).length
}
and than when you need to know object 's length you can run
exampleObject.length()
there's one problem with above, if you create an object that would have a "length" key, you will break the "prototype" function:
exampleObject : { length: 100, width: 150 }
but you can use your own synonym for length, like "l()" instead of "length()"