209

I'm sure it's somewhere inside the LoDash docs, but I can't seem to find the right combination.

var users = [{
      id: 12,
      name: 'Adam'
   },{
      id: 14,
      name: 'Bob'
   },{
      id: 16,
      name: 'Charlie'
   },{
      id: 18,
      name: 'David'
   }
]

// how do I get [12, 14, 16, 18]
var userIds = _.map(users, _.pick('id'));
Lacek
  • 1,595
  • 2
  • 11
  • 30
YarGnawh
  • 4,574
  • 6
  • 26
  • 37

8 Answers8

413

Since version v4.x you should use _.map:

_.map(users, 'id'); // [12, 14, 16, 18]

this way it is corresponds to native Array.prototype.map method where you would write (ES2015 syntax):

users.map(user => user.id); // [12, 14, 16, 18]

Before v4.x you could use _.pluck the same way:

_.pluck(users, 'id'); // [12, 14, 16, 18]
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • thanks much better than ```_.toArray(_.mapValues(users, 'id'))``` – YarGnawh Feb 05 '15 at 22:01
  • 1
    how can i get name with id, I mean how can we fetch more than two values ? – John Dec 09 '16 at 08:47
  • 8
    @John `users.map(({ id, name }) => ({ id, name }))` or less cryptic `users.map(user => ({ id: user.id, name: user.name }))`. Same in lodash: `_.map(users, ({ id, name }) => ({ id, name }))`. – dfsq Dec 09 '16 at 09:51
  • @dfsq, it's good solution while adding objects into output array. Thanks! – Ted Corleone Dec 22 '17 at 03:39
  • what if dere is a record multiple times in the original array? how to get unique only – Sami Ullah Sep 19 '22 at 12:57
22

In the new lodash release v4.0.0 _.pluck has removed in favor of _.map

Then you can use this:

_.map(users, 'id'); // [12, 14, 16, 18]

You can see in Github Changelog

iarroyo
  • 2,354
  • 24
  • 23
21

With pure JS:

var userIds = users.map( function(obj) { return obj.id; } );
c-smile
  • 26,734
  • 7
  • 59
  • 86
14

And if you need to extract several properties from each object, then

let newArr = _.map(arr, o => _.pick(o, ['name', 'surname', 'rate']));
Andrey
  • 1,752
  • 18
  • 17
10

Simple and even faster way to get it via ES6

let newArray = users.flatMap(i => i.ID) // -> [ 12, 13, 14, 15 ]
GYTO
  • 440
  • 1
  • 10
  • 23
  • Why `flatMap()` instead of `map()`? – Lacek Aug 14 '20 at 07:39
  • @Lacek `map()` is exist for object manipulation where `flatMap()` just for softing your Array to single array – GYTO Aug 25 '20 at 21:09
  • 3
    I know the different of `map()` and `flatMap()`. Just wanted to know why you suggested `flatMap()` in this case when there are already answers with `map()`. Would be great if you can elaborate when we should use `flatMap()` instead of `map()`. – Lacek Aug 26 '20 at 04:41
  • @Lacek Because is really useful in some cases to get basic data that you need from the array. Useful link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap – GYTO Aug 30 '20 at 00:19
-3

If you are using native javascript then you can use this code -

let ids = users.map(function(obj, index) {

    return obj.id;
})

console.log(ids); //[12, 14, 16, 18]
Pankaj Bisht
  • 986
  • 1
  • 8
  • 27
-3

const users = [{
      id: 12,
      name: 'Adam'
   },{
      id: 14,
      name: 'Bob'
   },{
      id: 16,
      name: 'Charlie'
   },{
      id: 18,
      name: 'David'
   }
]
const userIds = _.values(users);
console.log(userIds); //[12, 14, 16, 18]
  • OP wanted only the ids. Your code does not print out the ids, but the entire objects. – JW_ Feb 18 '21 at 15:45
-21

This will give you what you want in a pop-up.

for(var i = 0; i < users.Count; i++){
   alert(users[i].id);  
}
Ankur Verma
  • 5,793
  • 12
  • 57
  • 93
user1789573
  • 515
  • 2
  • 8
  • 23
  • 2
    It's to loop through the array one element at a time. An array is a data-structure of index(es) with values in it from some data-type (in this example it's objects). The objects can only be accessed by evaluating the elements in the index of the array. The loop is the structure that achieves this. – user1789573 Feb 05 '15 at 22:24