How can I get the object length ?
In console my object looks like this:
Object {
2: true,
3: true,
4: true
}
.length
will give me undefined. I just want to get the results = 3
for this case.
var keys = Object. keys(objInstance); var len = keys. length; Where len is your "length."
Answer: Use the Object. keys() Method You can simply use the Object. keys() method along with the length property to get the length of a JavaScript object. The Object. keys() method returns an array of a given object's own enumerable property names, and the length property returns the number of elements in that array.
Object. keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object . The ordering of the properties is the same as that given by looping over the properties of the object manually.
var keys = Object.keys(objInstance);
var len = keys.length;
Where len is your "length."
There may be circumstances I haven't thought of where this doesn't work, but it should do what you need, given your example.
Combining some of these answers into
app.filter('numkeys', function() {
return function(object) {
return Object.keys(object).length;
}
});
seems to work
<div ng-show="(furry.animals | numkeys) > 10">lots of furry animals</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With