I'm using Javascript and JSONP and want to find-out a user's total badge-count (i'm not using any libraries).

At first I naively added up the badge counts individually, but some user's have no badges. I also want to make sure it keeps working if the API changes to only list badge types the user has or a platinum badge is added.

I can't iterate through them either and userInfo.badge_counts.length is undefined.

What is the best way to do this?


The API (once it's version 1.0*, anyway) won't change. You're concerns are unwarranted.

Just sum the gold, silver, and bronze badges counts you get from /users/{id}.

*And with 0.9 being a release candidate, there almost certainly won't be any changes in badges of all things before 1.0

  • I know, but it makes sense (at least to me) to make it easier to upgrade when a new version does appear. Still, the main problem that prompted this question was that when I do that for some users (such as #999 on SuperUser) that don't have badges, the result I get is "NaN." – Gelatin Jun 26 '10 at 18:07
  • @Simon - those fields are marked as optional. You'll have to check for presence before summing across them. There are lots of places in the API where such checks are necessary. – Kevin Montrose Jun 26 '10 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .