Javascript 在 underscore.js 中按对象键查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8252550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
finding by object key in underscore.js
提问by Chin
I have the following object
我有以下对象
{ join: {} }
I'd like to find it's default object from the array below
我想从下面的数组中找到它的默认对象
[
{ login: { label: 'Login', url: '#login' } },
{ join: { label: 'Join', url: '#join', theme: 'a' } },
{ home: { label: 'none', icon: 'home', url: '#', theme: 'a' } }
]
I'd like to loop through the array and match the key, in this case 'join'
.
我想遍历数组并匹配键,在这种情况下'join'
。
This is what I have so far:
这是我到目前为止:
var butt_to_find = { join: {} }
var all_buttons = 'array above'
var matching = _.find(all_buttons, function(default_button){
return if default_butt key @ 1 is the same as butt_to_find key @ 1;
});
This is the first time I've used underscore after hearing so much about it. Any help, more than welcome
这是我在听了这么多之后第一次使用下划线。任何帮助,非常欢迎
回答by davidchambers
var buttons = [
{ login: { label: 'Login', url: '#login' } },
{ join: { label: 'Join', url: '#join', theme: 'a' } },
{ home: { label: 'none', icon: 'home', url: '#', theme: 'a' } }
]
_.find(buttons, function (button) { return 'join' in button })
The problem is that you're using a suboptimal data structure. This would make more sense, and produce simpler code:
问题是您使用的是次优数据结构。这会更有意义,并生成更简单的代码:
var buttons = {
login: {label: 'Login', url: '#login'},
join: {label: 'Join', url: '#join', theme: 'a'},
home: {label: 'none', icon: 'home', url: '#', theme: 'a'}
}
buttons.join // equivalent to the `_.find` line in the first example (but much simpler)
Perhaps you're using an array because the order of the buttons is important. In this case, I'd use an array of arrays:
也许您正在使用数组,因为按钮的顺序很重要。在这种情况下,我将使用一组数组:
var buttons = [
['login', {label: 'Login', url: '#login'}],
['join', {label: 'Join', url: '#join', theme: 'a'}],
['home', {label: 'none', icon: 'home', url: '#', theme: 'a'}]
]
_.find(buttons, function (button) { return button[0] === 'join' })
回答by ruakh
var matching =
( _.find
( all_buttons,
function (button)
{ return _.keys(butt_to_find)[0] in button;
}
)
);
where _.keys(butt_to_find)
evaluates to ['join']
(an array containing the keys of butt_to_find
), _.keys(butt_to_find)[0]
evaluates to 'join'
(the first element of said array), and _.keys(butt_to_find)[0] in button
evaluates to either true
or false
, depending whether button
contains 'join'
as a key. (The in
operatoris a regular JavaScript operator, not something added by underscore.js.)
where_.keys(butt_to_find)
计算为['join']
(包含 的键的数组butt_to_find
),_.keys(butt_to_find)[0]
计算为'join'
(所述数组的第一个元素),并_.keys(butt_to_find)[0] in button
计算为true
或false
,取决于是否button
包含'join'
作为键。(该in
操作是常规的JavaScript操作,不是通过添加underscore.js)。
回答by Shanimal
var def = {join: {}}
var defs = [
{ login: { label: 'Login', url: '#login' } },
{ join: { label: 'Join', url: '#join', theme: 'a' } },
{ home: { label: 'none', icon: 'home', url: '#', theme: 'a' } }
]
_.find(defs,function(item,key){
return _.has(item,_.keys(def)[0])
})
You can also switch to the lodash library (a drop in version of underscore) and do this
您还可以切换到 lodash 库(下划线版本的下降)并执行此操作
_.compact(_.pluck(defs,_.keys(def)[0]))