19

When I have a JavaScript object like this:

var member = {
    "mother": {
        "name" : "Mary",
        "age" : "48"
    },
    "father": {
        "name" : "Bill",
        "age" : "50"
    },
    "brother": {
        "name" : "Alex",
        "age" : "28"
    }
}

How to count objects in this object?!
I mean how to get a counting result 3, because there're only 3 objects inside: mother, father, brother?!

If it's not an array, so how to convert it into JSON array?

IARI
  • 1,217
  • 1
  • 18
  • 35
Nik Sumeiko
  • 8,263
  • 8
  • 50
  • 53

8 Answers8

32

That's not an array, is an object literal, you should iterate over the own properties of the object and count them, e.g.:

function objectLength(obj) {
  var result = 0;
  for(var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
    // or Object.prototype.hasOwnProperty.call(obj, prop)
      result++;
    }
  }
  return result;
}

objectLength(member); // for your example, 3

The hasOwnProperty method should be used to avoid iterating over inherited properties, e.g.

var obj = {};
typeof obj.toString; // "function"
obj.hasOwnProperty('toString'); // false, since it's inherited
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Might be worth checking for Mozilla's `__count__` property before iterating over the object... – James Apr 22 '10 at 17:34
  • 2
    @J-P: `__count__` is marked as obsolete for Gecko 1.9.3, (in Gecko 1.9.3a5pre, (Firefox 3.7a5pre) it doesn't exist anymore) – Christian C. Salvadó Apr 22 '10 at 17:37
  • 2
    @J-P, something that you could check before iterating, is the existence of the new ECMAScript 5th Edition [`Object.keys`](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/keys) method, ES5 is starting to be implemented by all major browser vendors, something like this: `if (typeof Object.keys === "function") return Object.keys(obj).length;` – Christian C. Salvadó Apr 22 '10 at 17:56
  • I feel like this is overdoing. If he's simply counting the number of properties in a JSON, relying on `Object.keys(memebers)` should be enough. – Frederico Apr 12 '20 at 23:13
  • Finally the solution for "Inside" methods, instead of documentation for Querries) – J A S K I E R Nov 30 '20 at 18:43
30

You can try this code, it works perfectly in a browser:

Object.keys(member).length;
Samim Hakimi
  • 705
  • 8
  • 22
Danieldms
  • 1,006
  • 9
  • 11
19

If you are using jquery on your page, this will work:

$(member).toArray().length;
TomZ
  • 421
  • 5
  • 6
7

You can use the Object.keys() method that returns an array of a given object's own enumerable properties:

Object.keys(member).length;
6

That is not an array, it is an object literal.

You can iterate the objects properties and count how many it owns:

var count = 0;
for (var k in obj) {
  // if the object has this property and it isn't a property
  // further up the prototype chain
  if (obj.hasOwnProperty(k)) count++;
}
gnarf
  • 105,192
  • 25
  • 127
  • 161
4
var member = {
        "mother": {
            "name" : "Mary",
            "age" : "48"
        },
        "father": {
            "name" : "Bill",
            "age" : "50"
        },
        "brother": {
            "name" : "Alex",
            "age" : "28"
        }
    }
    console.log('member key size:' + Object.keys(member).length);

ref a good guy : https://stackoverflow.com/questions/5223/length-of-a-javascript-object
LittleFi.F
  • 43
  • 4
1

Here's how I'd do it

function getObjectLength( obj )
{
  var length = 0;
  for ( var p in obj )
  {
    if ( obj.hasOwnProperty( p ) )
    {
      length++;
    }
  }
  return length;
}
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
0

In my practice I work like this:

/*
*   $.count(array) -  Returns the number of elements in an array (immproved equivalent to .length property). 
*/
$.count = function (array){
    if(array.length)
        return array.length;
    else
    {
        var length = 0;
        for ( var p in array ){if(array.hasOwnProperty(p)) length++;};
        return length;
    }
}
Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78