16

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.

ilim
  • 4,477
  • 7
  • 27
  • 46
godzo101
  • 237
  • 1
  • 5
  • 14
  • 2
    Possible duplicate of [How to get object length](http://stackoverflow.com/questions/5533192/how-to-get-object-length) – Durgpal Singh Feb 01 '17 at 05:01

8 Answers8

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

Aaron Averett
  • 896
  • 10
  • 12
  • 1
    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 – monstro Jan 31 '17 at 17:03
  • This appears to be a repeat of https://stackoverflow.com/questions/1345939/how-do-i-count-a-javascript-objects-attributes ... 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. – Rob Milliken Jun 17 '17 at 06:47
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>
commonpike
  • 10,499
  • 4
  • 65
  • 58
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>
kenny
  • 1,628
  • 20
  • 14
  • 2
    A better way to do this: `app.filter('objLength', function() { return function(object) { return Object.keys(object).length; } });` – Chris Sep 30 '16 at 14:29
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()"

mate.gvo
  • 1,093
  • 14
  • 20
  • 2
    It is [dangerous to directly extend Object.prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype) as all JavaScript objects use this prototype chain as part of JS core functionality and may create other problems. – Akin Williams Aug 09 '17 at 21:58
4

it can be get by below code simply

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

jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
2

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

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 : "";
 } 
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pecata
  • 683
  • 8
  • 13
-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.

Vivek S
  • 179
  • 1
  • 2
  • 17