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.
-
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 infor()
:( – Derek 朕會功夫 Commented Apr 13, 2012 at 2:08 -
Is there any reason you need to call
doThis
within the loop? The value ofkey
will be the value of the last key when the loop is finished, you know, since you usedvar
instead oflet
. – Ray Toal Commented Apr 13, 2012 at 2:16
3 Answers
Reset to default 8First 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.
-
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 infor()
:( – Derek 朕會功夫 Commented Apr 13, 2012 at 2:08 -
Is there any reason you need to call
doThis
within the loop? The value ofkey
will be the value of the last key when the loop is finished, you know, since you usedvar
instead oflet
. – Ray Toal Commented Apr 13, 2012 at 2:16
3 Answers
Reset to default 8First 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
版权声明:本文标题:javascript - Last loop in for()? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610536a2158977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论