0

There seems to have many question asked similar on counting number of element already but I am failing to implement them with mine problem.

After jquery ajax I get JSON data returned which looks something like this

Object {0: Object, 1: Object , xxxx:"asdf" ,yyyy:"asdf", zzzz:"asdf"}

I want to get number of object between this { } braces ( not counting those xxx,yyy element )

I tried .length which doesn't work

I also tried using this Length of a JavaScript object but that return the number of element in each object. I just want the number of object

Thank You

Community
  • 1
  • 1
cjmling
  • 6,896
  • 10
  • 43
  • 79
  • 1
    If it's not just a coincidence that the objects are the ones with numeric property names you could, assuming you have the top-level object in a variable, `obj`, do this: `for (var i=0; i in obj; i++) { /* use obj[i] */ }` - I don't really know why you need to count them, but obviously after that loop finishes `i` will be the count... – nnnnnn Dec 15 '12 at 06:17
  • Thank You this works well , enough to accept it as answer :) – cjmling Dec 15 '12 at 06:27
  • OK, I've posted it as an answer. – nnnnnn Dec 15 '12 at 06:31

4 Answers4

2

Try this:

var json = { 0: {}, 1: {}, xxxx: "asdf", yyyy: "asdf", zzzz: "asdf" };

function typeOf( obj ) {
  return ({}).toString.call( obj )
    .match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}

var total = 0;
for ( var o in json ) {
  if ( typeOf( json[o] ) == 'object' ) {
    total++;
  }
}

console.log( total ); //=> 2

Everything is an object in JavaScript. The typeof operator is misleading and won't work in this case. You can use the typeOf function above that I extracted from this blog post: Fixing the JavaScript typeof operator (worth reading). There are other ways of doing it but this seems like the most straightforward.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

If it's not just a coincidence that the objects are the ones with numeric property names, and the numeric properties count up sequentially, you could do something like this:

var obj = { /* your object here */ }
for (var i=0; i in obj; i++) {
   // use obj[i] for something
}
// i is now equal to the number of numeric properties

This works because as soon as i is high enough that you've run out of properties the in operator will return false. Feel free to use .hasOwnProperty() instead if you prefer.

Obviously this is a specialised solution that doesn't test the type of the different properties at all. (To actually test the type see elclanrs' solution - and either way read the page he linked to.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • yeah its object are the only one with numeric property names.. and therefore i think this answer will be easiest and best to my problem. Counting won't require also :) – cjmling Dec 15 '12 at 06:38
  • Thanks @elclanrs. When I noticed the pattern in the OP's object's properties this just kind of seemed right, bearing in mind all of the "this will only work if..." conditions. – nnnnnn Dec 15 '12 at 06:46
0

Say that the entire json is in a variable called json:

var total_objects = 0;
$.each(json, function () {
   if (typeof this == 'object') {
      total_objects++;
   }
});

However, I am curious as to why you would need to know this.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • I wanted to know this because I want to run an another loop to print each element in each of those object(not sure that is there another recommended way to do the same) :S – cjmling Dec 15 '12 at 06:14
  • Why do you need to count them first? Just run the print loop. – Barmar Dec 15 '12 at 06:15
  • This won't work. Strings are objects, arrays are objects, functions are objects...etc. `typeof` is misleading. – elclanrs Dec 15 '12 at 06:20
  • sorry but this doesn't seems to work...it count everything...I have 2 object inside and 36 of those xxx , yyy element... and therefore total_objects is giving me 38 :S – cjmling Dec 15 '12 at 06:20
0

You can use a customized version from the code of this question Length of Javascript Object (ie. Associative Array) and check for element's type using typeof operator and count only those which are an object (or an array).

Object.sizeObj = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (typeof key[obj] === 'object' && obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the count of those elements which are an object
var objectsCount = Object.sizeObj(myArray);
Community
  • 1
  • 1