javascript - Iterate over object, knowing when you're on the last iteration -
I need to be repeated on an object, but I know when I am on the last iteration. How can I do that? Objects do not have the "length" attribute, so I can not count the number of moving and compare it to the comparison length. I am tempted to do something
var len = 0; (Key in obj) {len ++; } Var i = 0; (Key in obj) {i ++; Var last_iter = (i == lane); ...} Is there a better way?
You can find the number of keys to be used:
Var n = object. Key (obje). Length A shim is available on pre-ES 5 browser.
The possible solution to your problem is:
var keys = Object.keys (obj); While (keys.length) {var key = keys.shift (); Var value = ojjj [key]; If (! Keys.length) {// it is the last key}}
Comments
Post a Comment