admin管理员组

文章数量:1410724

Consider the following:

var obj = {a:1, b:2, c:3, d:4, e:5, f:6};

Note that this is not an array (unless it is in JS and there is a way to map them)

I now have a requirement to get the next key, given a key. It also needs to be 'circular'. So findNext('c') should be d, while findNext('f') should be a

Here is what I came up with:

var obj = {a:1, b:2, c:3, d:4, e:5, f:6};
console.log ( findNext('e',obj), " is after e" );
console.log ( findNext('f',obj), " is after f" );
console.log ( findNext('a',obj), " is after a" );



function findNext (key,obj)
{
   var size = Object.keys(obj).length;
   var i;
   for (i=0; i<size; i++)
   {
      if (Object.keys(obj)[i] == key)
      break;
   }
   i = (i + 1) % size;
   return Object.keys(obj)[i];
   
}

Consider the following:

var obj = {a:1, b:2, c:3, d:4, e:5, f:6};

Note that this is not an array (unless it is in JS and there is a way to map them)

I now have a requirement to get the next key, given a key. It also needs to be 'circular'. So findNext('c') should be d, while findNext('f') should be a

Here is what I came up with:

var obj = {a:1, b:2, c:3, d:4, e:5, f:6};
console.log ( findNext('e',obj), " is after e" );
console.log ( findNext('f',obj), " is after f" );
console.log ( findNext('a',obj), " is after a" );



function findNext (key,obj)
{
   var size = Object.keys(obj).length;
   var i;
   for (i=0; i<size; i++)
   {
      if (Object.keys(obj)[i] == key)
      break;
   }
   i = (i + 1) % size;
   return Object.keys(obj)[i];
   
}

Is there a better way to do this, given that all keys are a hash? Can I avoid not having to iterate till X to find next(X) ?

thanks

Share asked Jan 8, 2017 at 0:16 user1361529user1361529 2,69733 silver badges64 bronze badges 3
  • define "next". Order of object properties is not consistent depending on how they are constructed and can not be relied upon – charlietfl Commented Jan 8, 2017 at 1:14
  • why not just do a Object.keys(obj).length and if size == Object.keys(obj).length - 1, size = 0? – A. L Commented Jan 8, 2017 at 1:30
  • Not quite sure what happened to the answers and ments that were posted to this Q -they disappeared - maybe because someone chose to vote to close this question? charlietfl - yes, I agree - next would be any one that is next in that particular system when iterating. A. Lau: Thats what I'm doing in the 2nd to last line? ( % instead of the if) – user1361529 Commented Jan 8, 2017 at 1:34
Add a ment  | 

2 Answers 2

Reset to default 6

Using

  • Object.keys
  • Array.indexOf
  • Arithmetic operators - Remainder

function findNext(key, obj) {
  var keys = Object.keys(obj);

  console.log(
    keys[(keys.indexOf(key) + 1) % keys.length]
  );
}

var obj = {a:1, b:2, c:3, d:4, e:5, f:6};

findNext('a', obj); // b
findNext('c', obj); // d
findNext('f', obj); // a

How about this, Although you'll need to handle the else, also I am using es6 but you can paste on babel.io to get the es5 output

let obj = {a:1, b:2, c:3, d:4, e:5, f:6};

const findNext = (key, obj) => {
  const keys = Object.keys(obj);
  const len = Object.keys(obj).length;

  if (keys.indexOf(key) > -1) {
     const nextIndex = (keys.indexOf(key) + 1) % len;
     const next = keys[nextIndex];

     return { [next]: obj[next] };
  }
}

本文标签: getting the next key in a set of keys (circular) in a Javascript objectStack Overflow