211

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'));

8 Answers 8

416

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]
Sign up to request clarification or add additional context in comments.

5 Comments

thanks much better than _.toArray(_.mapValues(users, 'id'))
how can i get name with id, I mean how can we fetch more than two values ?
@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, it's good solution while adding objects into output array. Thanks!
what if dere is a record multiple times in the original array? how to get unique only
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

Comments

21

With pure JS:

var userIds = users.map( function(obj) { return obj.id; } );

2 Comments

You can simplify this into var userIds = users.map( obj => obj.id );
Get on my level users.map(({id})=>id)
17

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

let newArr = _.map(arr, o => _.pick(o, ['name', 'surname', 'rate']));

1 Comment

Hello, is there any lodash method to achieve this?
11

Simple and even faster way to get it via ES6

let newArray = users.flatMap(i => i.ID) // -> [ 12, 13, 14, 15 ]

4 Comments

Why flatMap() instead of map()?
@Lacek map() is exist for object manipulation where flatMap() just for softing your Array<Object> to single array
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 Because is really useful in some cases to get basic data that you need from the array. Useful link developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-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]

1 Comment

The OP explicitly asked for a Lodash solution.
-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]

1 Comment

OP wanted only the ids. Your code does not print out the ids, but the entire objects.
-21

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

for(var i = 0; i < users.Count; i++){
   alert(users[i].id);  
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.