admin管理员组

文章数量:1024582

I have been struggling with this for a while:

for( var key in obj ){
    blah.blah()
    if(choice){
         doThis();
    }
}

How can I call doThis() only at the last item? Since I don't have a counter, i, I can't determine which is the last item.

I have been struggling with this for a while:

for( var key in obj ){
    blah.blah()
    if(choice){
         doThis();
    }
}

How can I call doThis() only at the last item? Since I don't have a counter, i, I can't determine which is the last item.

Share Improve this question edited Apr 13, 2012 at 2:11 James Sumners 14.8k11 gold badges61 silver badges77 bronze badges asked Apr 13, 2012 at 2:03 Derek 朕會功夫Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges 3
  • Is there a reason why you can't use an interator i? – Marc Commented Apr 13, 2012 at 2:07
  • I am looping through an object. And i isn't defined in for() :( – Derek 朕會功夫 Commented Apr 13, 2012 at 2:08
  • Is there any reason you need to call doThis within the loop? The value of key will be the value of the last key when the loop is finished, you know, since you used var instead of let. – Ray Toal Commented Apr 13, 2012 at 2:16
Add a ment  | 

3 Answers 3

Reset to default 8

First off, keys don't e in any guaranteed order. By specification, the keys are unordered. If you really wanted to do something with the last key you got, you could do so like this;

var lastkey;
for( var key in obj ){
    lastkey = key;
    blah.blah()
}
if(choice){
    // use lastkey if you want
    doThis();
}

With the caveat that jfriend00 mentioned in his answer that keys do not e in a guaranteed order, here is the most general transformation to augment:

  • an arbitrary for-loop
  • an arbitrary while-loop
  • an arbitrary callback-based iterator like [].forEach

In the core of the loop block, our variable isLast will be true if it's the last, or false if it isn't:

var toDo = function(){};
forOrWhile(...) {
    toDo(false);
    toDo = function(isLast) {
        // blah.blah(); console.log('visited key '+key+' with value '+obj[key]+' where isLast='+isLast)
    };
}
toDo(true)

(If you need to capture the value of the iterator, you will need to put it in a closure as usual in javascript (Google for javascript for loop closure). You would get this for free if it was an Array, then you could use [].map(function(){...}) or .forEach(function(){...}), and this is why frameworks's foreach(object) equivalents are useful; they will also usually filter out "prototype garbage" with .hasOwnProperty.)

The code you've given would be translated to:

for( var key in obj ){
    blah.blah()
}
doThis();

If this doesn't work, please explain why in your question.

I have been struggling with this for a while:

for( var key in obj ){
    blah.blah()
    if(choice){
         doThis();
    }
}

How can I call doThis() only at the last item? Since I don't have a counter, i, I can't determine which is the last item.

I have been struggling with this for a while:

for( var key in obj ){
    blah.blah()
    if(choice){
         doThis();
    }
}

How can I call doThis() only at the last item? Since I don't have a counter, i, I can't determine which is the last item.

Share Improve this question edited Apr 13, 2012 at 2:11 James Sumners 14.8k11 gold badges61 silver badges77 bronze badges asked Apr 13, 2012 at 2:03 Derek 朕會功夫Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges 3
  • Is there a reason why you can't use an interator i? – Marc Commented Apr 13, 2012 at 2:07
  • I am looping through an object. And i isn't defined in for() :( – Derek 朕會功夫 Commented Apr 13, 2012 at 2:08
  • Is there any reason you need to call doThis within the loop? The value of key will be the value of the last key when the loop is finished, you know, since you used var instead of let. – Ray Toal Commented Apr 13, 2012 at 2:16
Add a ment  | 

3 Answers 3

Reset to default 8

First off, keys don't e in any guaranteed order. By specification, the keys are unordered. If you really wanted to do something with the last key you got, you could do so like this;

var lastkey;
for( var key in obj ){
    lastkey = key;
    blah.blah()
}
if(choice){
    // use lastkey if you want
    doThis();
}

With the caveat that jfriend00 mentioned in his answer that keys do not e in a guaranteed order, here is the most general transformation to augment:

  • an arbitrary for-loop
  • an arbitrary while-loop
  • an arbitrary callback-based iterator like [].forEach

In the core of the loop block, our variable isLast will be true if it's the last, or false if it isn't:

var toDo = function(){};
forOrWhile(...) {
    toDo(false);
    toDo = function(isLast) {
        // blah.blah(); console.log('visited key '+key+' with value '+obj[key]+' where isLast='+isLast)
    };
}
toDo(true)

(If you need to capture the value of the iterator, you will need to put it in a closure as usual in javascript (Google for javascript for loop closure). You would get this for free if it was an Array, then you could use [].map(function(){...}) or .forEach(function(){...}), and this is why frameworks's foreach(object) equivalents are useful; they will also usually filter out "prototype garbage" with .hasOwnProperty.)

The code you've given would be translated to:

for( var key in obj ){
    blah.blah()
}
doThis();

If this doesn't work, please explain why in your question.

本文标签: javascriptLast loop in for()Stack Overflow