I have:
Object {12: Object, 13: Object, 14: Object}
and I want to detect the last for loop step in here:
for (var i in objs){
console.log(objs[i]);
}
any ideas?
I have:
Object {12: Object, 13: Object, 14: Object}
and I want to detect the last for loop step in here:
for (var i in objs){
console.log(objs[i]);
}
any ideas?
you could use standard object method Object.keys();
obj = {12: Object, 13: Object, 14: Object};
keys = Object.keys(obj);
console.log(keys[keys.length - 1]);
console.log(obj[keys[keys.length - 1]]);//if you want that last ..
I hope this what you want
var latest;
for (var i in objs){
console.log(objs[i]);
latest = objs[i];
}
//work with latest
Since you cannot control properties' order of an object (they come in arbitrary order which you cannot define) the whole idea probably loses some, or most, if not all, of its relevance.
If you don't actually care which property is being processed in that last loop step, here's an example of how you could go:
var objs = {12: Object, 13: Object, 14: Object};
var c = 0;
var objsKeys = Object.keys(objs); [12, 13, 14]
for (var i=0; i<obsjKeys.length, i++){
if (i===(obsjKeys.length -1)) {
console.log("Last Run of the Loop:");
}
console.log(objs[obsjKeys[i]]);
}
Object.keys()
is only available since ES5, which means IE < 9 will not support it. In that case you can do your own implementation of it using for..in
:
Object.keys = Object.keys || function(obj) {
var tempArray = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
tempArray.push(key);
}
}
return tempArray;
};