获取对象中的下一个键值对 [英] Get next key-value pair in an object
问题描述
给定一个键,我想找到一个对象中的下一个属性。我不能依靠按键来排序或顺序(他们是uuids)。请参考下面的例子:
var db = {
或它是最后一个,
a:1,
b :2,
c:3
}
var next = function(db,key){
//
}
next(db,'a'); //我想要2
next(db,'b'); //我想要一个prev()函数,但我敢肯定,它会是一个简单的函数。是相同的解决方案。
这似乎是一个微不足道的问题,但我不能为我的生活弄清楚如何做。
对于使用underscore.js的解决方案或写在coffeescript中的解决方法很高兴)。
解决方案正确的答案是:您不能这样做,因为对象是无序的,如ECMAScript的规范。
我建议您使用有序结构(如数组)来解决问题:
var db = [
{key:'a',value:1},
{key:'b',value:2 },
{key:'c',value:3}
];
然后
下一个
:var next = function(db,key){
for(var i = 0; iif(db [i] .key === key){
return db [i + 1]&& db [i + 1] .value;
}
}
};
如果
键
code> dbnext
返回undefined
。如果你永远不会要求下一个最后一个项目,你可以通过删除三元&&
运算符并返回db [i + 1] .value
。
c> next 更简单:
var next = function(db,key){
var i = _.pluck(db,'key')。indexOf(key);
return i!== -1&&& db [i + 1]&& db [i + 1] .value;
};
(在这种情况下
next
code> false 有时...但它仍然是一个假值:))
现在,一个更务实的答案可能是,因为大多数浏览器会尊重一个对象在迭代时初始化的顺序,你可以使用
在
循环,因为其他答案建议。我建议使用Object.keys
来简化迭代数组的工作://假设db是一个对象在问题中定义。
var next = function(db,key){
var keys = Object.keys(db)
,i = keys.indexOf(key)
return i!== -1&&&键[i + 1]&& db [keys [i + 1]];
};
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 :)
解决方案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 ondb
or it was the last one,next
returnsundefined
. 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 returningdb[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 returnfalse
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 usingObject.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]]; };
这篇关于获取对象中的下一个键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!