Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About underscore.map

I have the following object

{one : 1, two : 2, three : 3}

and I would like

[1,2]

Here my code

_.map({one : 1, two : 2, three : 3}, function(num, key){ 
         if (key==='one' || key==='two') {
             return num;
         } 
}); // [1, 2, undefined]

Actually I would like [1,2]

How can I improve my code?
thanks

like image 549
js999 Avatar asked May 24 '12 07:05

js999


1 Answers

You actually want to use _.pick and _.values:

_.values( _.pick( obj, "one", "two" ) )
like image 74
Jordan Running Avatar answered Oct 27 '22 05:10

Jordan Running