Identifying the last item in a JavaScript object or associative array -


i'm putting javascript that'll pulling in multiple json files data used in graphs. ultimately, need identify when i've reached absolute end of associative array being processed in $.each() loop (jquery involved) in order trigger next json file. i've bumped various solutions one-tiered object...but multi-tiered object? how last row in last column know it's last?

here's example json i'm working with:

{     "network":{         "hardware":262464,         "software":53016,         "internal personnel":440202,         "external personnel":188658,         "outside services":1344100,         "facilities , overhead":16172,         "other":92588,         "total":2397200     },     "wide area network":{         "hardware":75600,         "software":18900,         "internal personnel":132300,         "external personnel":56700,         "outside services":315000,         "facilities , overhead":6300,         "other":25200,         "total":630000     } } 

here's rough idea of loop i'm running on json

function buildcolumns(array, parent) {     $.each(array, function(key, value) {         var newparent = $('<column>');          $('<columnbox>').append($('<h2>').html(key)).append(newparent).appendto(parent);         if(value instanceof object) buildcolumns(value, newparent);          }); } 

the loop pseudo-recursive, can see, , may not use. there go.

you can find out how many items in array length property.

so in example array.length return number of items. that's assuming passed variable array.

the way can objects is:

object.keys(jsonobject).length 

also, because wasn't sure if array or object trying find length off, can use code below add count method object find how many children has:

object.prototype.count = function() {   ocount = 0;    for(var curobj in this)      if(curobj.hasownproperty(prop)) ocount++;    return ocount; }; 

to used such:

console.log(jsonobject.count()); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -