34

I am trying to get an object in an array by the value of one of its keys.

The array:

var arr = [
        {
            city: 'Amsterdam',
            title: 'This is Amsterdam!'
        },
        {
            city: 'Berlin',
            title: 'This is Berlin!'
        },
        {
            city: 'Budapest',
            title: 'This is Budapest!'
        }
];

I tried doing something like this with lodash but no success.

var picked = lodash.pickBy(arr, lodash.isEqual('Amsterdam');

and it returns an empty object.

Any idea on how I can do this the lodash way (if it's even possible)? I can do it the classic way, creating a new array, looping through all objects and pushing the ones matching my criteria to that new array. But is there a way to do it with lodash?

This is NOT a duplicate.

Ariel Weinberger
  • 561
  • 1
  • 6
  • 12
  • @JoachimIsaksson Why can't you post an answer? It's exactly what I was looking for! I would mark it as answered – Ariel Weinberger Mar 28 '16 at 10:07
  • @Rajesh because of the uniform principle. I use lodash for all object/array manipulations in my Node application and if possible - I would like to use it where possible. – Ariel Weinberger Mar 28 '16 at 10:08
  • 1
    Yes, none of the 10M questions on SO has ever asked how to find the element of an array of objects based on one of the objects' properties. If you don't like the question this is marked as a duplicate of, try http://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects. –  Mar 28 '16 at 10:22
  • @torazaburo For something to be a duplicate of something else, it has to be identical. None of these questions involve any lodash. I was asking specifically for lodash, therefore this is not a duplicate. – Ariel Weinberger Mar 28 '16 at 10:29
  • 1
    The link I gave contains an answer for underscore, which is pretty much identical to lodash, and a comment on that answer specifically references lodash. Instead of quibbling over the definition of duplicate, I suggest you simply browse through the lodash docs, and you will quickly find the answer to your question. For instance, you would soon find the `_.find` routine. You would also find an example which matches your use case exactly, which is `_.find(users, _.matchesProperty('city', 'Amsterdam'));`. It's always been a mystery to me why people think SO is a "read the docs for me" service. –  Mar 28 '16 at 10:43
  • Your question entered a review queue for reopening because it was edited. It was decided, [3 votes against 2](http://stackoverflow.com/review/reopen/11797188) that it be left closed. If you think it's not a duplicate, explain why rather than just stating it. – Wai Ha Lee Mar 28 '16 at 10:53

4 Answers4

102

You can use Array.prototype.find() with pure javascript:

var picked = arr.find(o => o.city === 'Amsterdam');

It is currently not compatible with all browsers, you need to check it in your environment (but it should work in NodeJS).

madox2
  • 49,493
  • 17
  • 99
  • 99
17

Using lodash and an arrow function, it should be as simple as;

var picked = lodash.filter(arr, x => x.city === 'Amsterdam');

...or alternately with object notation;

var picked = lodash.filter(arr, { 'city': 'Amsterdam' } );

Note: The above answer used to be based on pickBy, which as @torazaburo points out below was not a good choice for the use case.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • `pickBy` is not what he wants here. `pickBy` picks certain properties matching the predicate. It will create an object instead of a filtered array. –  Mar 28 '16 at 10:19
7

classical way is even simpler

try

var output = arr.filter(function(value){ return value.city=="Amsterdam";})
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
2

You can use Array.filter

As correctly pointed by @torazaburo, you do not need ternary operator return item[key]?item[key] === value:false. A simple check return item[key] === value will do fine.

var arr = [{
  city: 'Amsterdam',
  title: 'This is Amsterdam!'
}, {
  city: 'Berlin',
  title: 'This is Berlin!'
}, {
  city: 'Budapest',
  title: 'This is Budapest!'
}];

Array.prototype.findByValueOfObject = function(key, value) {
  return this.filter(function(item) {
    return (item[key] === value);
  });
}

document.write("<pre>" + JSON.stringify(arr.findByValueOfObject("city", "Amsterdam"), 0, 4) + "</pre>");
Rajesh
  • 24,354
  • 5
  • 48
  • 79