Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:58:13+00:00 2026-06-11T13:58:13+00:00

Given a key, I want to find the next property in an object. I

  • 0

Given a key, I want to find the next property in an object. 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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:58:14+00:00Added an answer on June 11, 2026 at 1:58 pm

    The correct answer is: you can’t do that, as objects are unordered as per ECMAScript’s spec.

    I’d recommend that you use an ordered structure, like an array, for the purpose of the problem:

    var db = [
      {key: 'a', value: 1},
      {key: 'b', value: 2},
      {key: 'c', value: 3}
    ];
    

    Then the next function can be something like:

    var next = function(db, key) {
      for (var i = 0; i < db.length; i++) {
        if (db[i].key === key) {
          return db[i + 1] && db[i + 1].value;
        }
      }
    };
    

    In case key does not exist on db or it was the last one, next returns undefined. if you’re never going to ask for the next of the last item, you can simplify that function by removing the ternary && operator and returning db[i + 1].value directly.

    You can also use some of Underscore.js utility methods to make next simpler:

    var next = function(db, key) {
      var i = _.pluck(db, 'key').indexOf(key);
      return i !== -1 && db[i + 1] && db[i + 1].value;
    };
    

    (in this case next could return false sometimes… but it’s still a falsy value :))


    Now, a more pragmatic answer could be that, as most browsers will respect the order in which an object was initialized when iterating it, you can just iterate it with a for in loop as the other answers suggest. I’d recommend using Object.keys to simplify the job of iterating over the array:

    // Assuming that db is an object as defined in the question.
    var next = function(db, key) {
      var keys = Object.keys(db)
        , i = keys.indexOf(key);
      return i !== -1 && keys[i + 1] && db[keys[i + 1]];
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can ItemCheckedEventArgs.Item.Selected = ItemCheckedEventArgs.Item.Checked generate The given key was not present in the
How to find corresponding WMI/COM object for any given Windows Registry key? Is there
I want to find out the key for given value from HashMap, currently I
I want to print a key from a given hash key but I can't
Given the following dictionary, I want to find out which key had the most
I can't find a way to remove keys from a hash that are not
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Given an Object: myObj = {key : 'value'} How do I get the key?
Given that the Rails Way seems to be not to use foreign key constraints,
Given: CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id)); I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.