Title: Iterate over object, knowing when you're on the last iteration
Tags: javascript
Question: I need to iterate over an Object, but know when I'm on the last iteration.  How can I do that?  Objects don't have a "length" attribute, so I can't just count the iteration number and compare it to the length.  I'm tempted to do something like
var len = 0;
for ( key in obj ) {
len++;
var i = 0;
for ( key in obj ) {
i++;
var last_iter = (i == len);
...
Is there a better way?
Comment: @TravisJ: JSON is text, so if you mean enumerating the characters of a string, then you may be right.

Comment: @TravisJ: Well at that point it's no longer JSON, but I'd be curious to see what you mean. Shouldn't be a problem if it's documented. There's no difference between an object created after some JSON was parsed and any other plain object.

Comment: @TravisJ: Please describe the difference. Is it a bug in `JSON.parse`?

Comment: @AlmirSarajčić: What are you talking about?

Comment: @TravisJ: Well anyway... the only thing I can find is a older *(now resolved)* bug that affected `JSON.parse` in IE8, but as far as I can tell, it affects the parsing itself, not the `for-in` enumeration. And jQuery's `$.each` doesn't do anything special. It's just that the bug doesn't manifest itself when `Array.prototype` and `Function.prototype` have not been extended.

Comment: @TravisJ: Well, the effect of extending prototypes would be the same in any browser *(with respect to `for-in`)*. Extending `Array.prototype` would only affect `for-in` if you're using it on an Array, which usually shouldn't be done.

Comment: Side note: `for( a in b )` will not work in ie8- with json objects. Highly suggest using `$.each` for those situations or an equivalent workaround.

Comment: @user1689607 - I have found that using a for in loop in ie8 and below will throw an error when the object in question is json.

Comment: @user1689607 - a properly formed json object, not it's string version.

Comment: @user1689607 - That is what I thought too, but there is a difference.

Comment: @user1689607 - You make a very valid point about the prototypes being extended and a negative correlation to `for in` for ie.

Comment: It's ok guys, you are both smart.

Comment: @TravisJ You're going to have to post some code to backup your claims.

Comment: It is worth point out that objects aren't necessarily always in the same specified order. They don't have an order per se, like Arrays do. So all you can know is that you have processed all but 1 remaining key, but there isn't such at thing as "last" really.

Here is the accepted answer: You can find the number of keys using:
var n = Object.keys(obj).length
On pre-ES5 browsers a shim is available on the MDN website.
A possible solution to your problem is this:
var keys = Object.keys(obj);
while (keys.length) {
var key = keys.shift();
var value = obj[key];
if (!keys.length) {
// this is the last key
Comment for this answer: @elclanrs I was just about to add that :)

Here is another answer: If you need to do something with the last key, you could simply set it into a variable and use it after the loop. This would save looping around twice.
var key = '';
for (key in obj) {
//...
// Operation with last key...
// obj[key];
Comment for this answer: I'm not sure what the intention of the OP is.
Comment for this answer: You are overdoing it - `key` is automatically set to the last key when the loop exits. Plus I assumed he wanted to avoid part of the code for the last key.

Here is another answer: var prev_key = false;
for (key in obj) {
if(prev_key) {
<code here&gt;
prev_key = key;
<code here for last key&gt;
This code would work better if you could get the first key of the object, so you could avoid the if, but you can't do that reliably in javascript (and still iterate in the same order in the loop).

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 14, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/01/12', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript>', 'Title': "Iterate over object, knowing when you're on the last iteration", 'ViewCount': 103, 'id': 13185962, 'user_AboutMe': '', 'user_AccountId': 2786.0, 'user_DisplayName': 'Martin C. Martin', 'user_Reputation': 3493.0, 'user_Views': 455.0, 'user_id': 3917.0}}