Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

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

like image 913
Chin Avatar asked Nov 24 '11 05:11

Chin


2 Answers

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' })
like image 77
davidchambers Avatar answered Oct 17 '22 07:10

davidchambers


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 operator is a regular JavaScript operator, not something added by underscore.js.)

like image 22
ruakh Avatar answered Oct 17 '22 07:10

ruakh