17

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.

1

8 Answers 8

46
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This is strange, but the key moment here is that you need to get keys first and assign it to a variable, only then, you can get the length. Object.keys(objInstance).length will not work
This appears to be a repeat of stackoverflow.com/questions/1345939/… ... not sure why monstro says Object.keys(objInstance).length won't work, though I do see comments in that link I provided that indicate it may not work with older (IE8 and below) browsers. Try it with a modern browser and you may be surprised at the results.
11

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>

1 Comment

Just perfect ;)
6

You could also use a filter

Edit:

app.filter('objLength', function() { 
 return function(object) { 
  return Object.keys(object).length; 
 } 
});

Old Answer:

app.filter('objLength', function() {
 return function(object) {
  var count = 0;

  for(var i in object){
   count++;
  }
  return count;
 }
});

And then use it maybe like this

<div ng-hide="(navigations._source.languages | objLength) == 1"> content</div>

1 Comment

A better way to do this: app.filter('objLength', function() { return function(object) { return Object.keys(object).length; } });
4

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()"

1 Comment

It is dangerous to directly extend Object.prototype as all JavaScript objects use this prototype chain as part of JS core functionality and may create other problems.
4

it can be get by below code simply

var itemsLength = Object.keys(your object).length;

3 Comments

This works in console, but not in angular app, see the answer above.
you can get object length in javascript or javascript based libs in your apps
2

Short variant Object.keys($scope.someobject).length

Comments

0

We can also add a small validation if we are retrieving data from API.

app.filter('objLength', function() { 
 return function(object) { 
  return object ? Object.keys(object).length : "";
 } 
});

Comments

-1

Even I don't understand why in Angular markup not able to get the value of Objects via 'Object.keys'. But here there's some easy solution. Check it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.