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

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -