识别 Javascript 对象中的最后一次迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4478685/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recognize the last iteration in a Javascript object
提问by Omar Abid
I have an object that I'm iterating
我有一个正在迭代的对象
for (el in object) {
// Some work here
}
I want to know when is the last iteration, inside the iteration, so I can do
我想知道最后一次迭代是什么时候,在迭代内部,所以我可以做
for (el in object) {
// Some work here
if (last_iteration) {
// Do something
}
}
Any straightforward way to do it?
有什么直接的方法可以做到吗?
采纳答案by icyrock.com
You can do something like this:
你可以这样做:
var first = true;
var prev;
for (var el in object) {
  // Some work here
  if (first) {
    first = false;
  } else {
    doSomething(prev, object[prev]);
  }
  prev = el;
}
if (prev !== undefined) { // There was at least one element
  doSomethingElse(prev, object[prev]); // Prev is now last of all elements
}
This is in case you want to process all but the last element in one way (doSomething) and process the last element in another way (doSomethingElse).
如果您想以一种方式 ( doSomething) 处理除最后一个元素之外的所有元素,并以另一种方式 ( ) 处理最后一个元素doSomethingElse。
If you want to process all the elements in one way (doSomething) and want to have extra processing for the last element only (doSomethingExtra), you can do:
如果您想以一种方式 ( doSomething)处理所有元素,并且只想对最后一个元素 ( doSomethingExtra)进行额外处理,您可以执行以下操作:
var prev;
for (var el in object) {
  // Some work here
  doSomething(el, object[el]);
  prev = el;
}
if (prev !== undefined) { // There was at least one element
  doSomethingExtra(prev, object[prev]); // Prev is now last of all elements
}
To make it even shorter, you can do similar to what T?r?k Gábor did in the gist he provided, by reusing elvariable, i.e.:
为了使它更短,您可以通过重用变量来执行类似于 T?r?k Gábor 在他提供的要点中所做的操作el,即:
var el;
for (el in object) {
  // Some work here
  doSomething(el, object[el]);
}
if (el !== undefined) { // There was at least one element
  doSomethingExtra(el, object[el]); // El is now last of all elements
}
Hope this helps.
希望这可以帮助。
回答by Marcel
I know I'm late but I just ran into this and fixed it like this:
我知道我迟到了,但我刚刚遇到了这个问题并像这样修复了它:
var i = 0;
var object = { a: 1, b: 2 };
var length = Object.keys(object).length;
for (el in object) {
   var last = i === length - 1; // true if last, false if not last
   console.log(i, el, last);
   i++;
}
回答by Pablo Fernandez
Note that this will only work if the object you are iterating over is an array (has numeric keys)
请注意,这仅在您迭代的对象是数组(具有数字键)时才有效
var a = [1,2,3,4,5];
for (i in a) {
  if(a[+i+1] === undefined) 
    console.log('the last one is: ' + a[i]);
}
Note that the +sign before iis necessary since if omitted, it will do a string concatenation, the keys resulting in 01, 12, 23, etc
请注意,在+签收前i是必要的,因为如果忽略,它会做一个字符串连接时,密钥产生01,12,23,等
回答by Free Consulting
as said already, there is no distinct order for properties, so last enumerated property is only known afterwards.
如前所述,属性没有明确的顺序,所以最后枚举的属性只有在之后才知道。
var object = { a: 'b', c: 42 };
for ( var string in object ) ;
alert( object[string] );  // last property name is still here 

