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
andif 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
2 Answers
Reset to default 6Using
- 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
版权声明:本文标题:getting the next key in a set of keys (circular) in a Javascript object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745017965a2637957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论