Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript counting number of objects in object [duplicate]

I have an object something like:

Object {0=Object, 1=Object, 2=Object} // Output from console.log(obj.Data); 

But there is no way that I can count the number of objects in object, then finally get the attribute value from the sub objects.

I have tried

console.log(obj.Data[0].length); // It does not work  console.log(obj.Data.length); // It does not work 

This is a bit tricky for me. Hope you guys can help.

like image 338
Bryan Learn Avatar asked Jun 07 '13 05:06

Bryan Learn


People also ask

How do I count occurrence of duplicate items in array?

php $array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21); //$previous[value][Occurrence] for($arr = 0; $arr < count($array); $arr++){ $current = $array[$arr]; for($n = 0; $n < count($previous); $n++){ if($current != $previous[$n][0]){// 12 is not 43 -----> TRUE if($current !=


1 Answers

The easiest way to do this, with excellent performance and compatibility with both old and new browsers, is to include either Lo-Dash or Underscore in your page.

Then you can use either _.size(object) or _.keys(object).length

For your obj.Data, you could test this with:

console.log( _.size(obj.Data) ); 

or:

console.log( _.keys(obj.Data).length ); 

Lo-Dash and Underscore are both excellent libraries; you would find either one very useful in your code. (They are rather similar to each other; Lo-Dash is a newer version with some advantanges.)

Alternatively, you could include this function in your code, which simply loops through the object's properties and counts them:

function ObjectLength( object ) {     var length = 0;     for( var key in object ) {         if( object.hasOwnProperty(key) ) {             ++length;         }     }     return length; }; 

You can test this with:

console.log( ObjectLength(obj.Data) ); 

That code is not as fast as it could be in modern browsers, though. For a version that's much faster in modern browsers and still works in old ones, you can use:

function ObjectLength_Modern( object ) {     return Object.keys(object).length; }  function ObjectLength_Legacy( object ) {     var length = 0;     for( var key in object ) {         if( object.hasOwnProperty(key) ) {             ++length;         }     }     return length; }  var ObjectLength =     Object.keys ? ObjectLength_Modern : ObjectLength_Legacy; 

and as before, test it with:

console.log( ObjectLength(obj.Data) ); 

This code uses Object.keys(object).length in modern browsers and falls back to counting in a loop for old browsers.

But if you're going to all this work, I would recommend using Lo-Dash or Underscore instead and get all the benefits those libraries offer.

I set up a jsPerf that compares the speed of these various approaches. Please run it in any browsers you have handy to add to the tests.

Thanks to Barmar for suggesting Object.keys for newer browsers in his answer.

like image 55
Michael Geary Avatar answered Sep 19 '22 18:09

Michael Geary