Get next key-value pair in an object

Posted by captainclam on Stack Overflow See other posts from Stack Overflow or by captainclam
Published on 2012-09-20T03:10:36Z Indexed on 2012/09/20 3:37 UTC
Read the original article Hit count: 149

So, given a key, I want to find the next property in an object. Then, I want to return the value of the NEXT property. I can not rely on the keys to be ordered or sequential (they're uuids). Please see below for trivial example of what I want:

var db ={
  a: 1,
  b: 2,
  c: 3
}

var next = function(db, key) {
  // ???
}

next(db, 'a');  // I want 2
next(db, 'b');  // I want 3

I also want a prev() function, but I'm sure it will be the same solution.

This seems like such a trivial problem but I can't for the life of me figure out how to do it.

Happy for the solution to use underscore.js or be written in coffeescript :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON