简体   繁体   English

在underscore.js中按对象键查找

[英]finding by object key in underscore.js

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 任何帮助,超过欢迎

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' })
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. 其中_.keys(butt_to_find)求值为['join'] (包含butt_to_find键的数组), _.keys(butt_to_find)[0] in button _.keys(butt_to_find)[0]求值为'join' (所述数组的第一个元素)和_.keys(butt_to_find)[0] in button计算结果为truefalse ,具体取决于button是否包含'join'作为键。 ( The in operator is a regular JavaScript operator, not something added by underscore.js.) in运算符是常规JavaScript运算符,而不是由underscore.js添加的。)

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]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM