15

I'm trying to get all objects with matching id's from my students array and get other property values from them...

For instance my array looks like this:

const students = [
    {id: 1, name: 'Cal', location: 'McHale' },
    {id: 2, name: 'Courtney', location: 'Sydney Hall' }, 
    {id: 1, name: 'Cal', location: 'Syndey hall' }
]

So my expected output would grab all instances of id: 1.

{id: 1, name: 'Cal', location: 'McHale' },
{id: 1, name: 'Cal', location: 'Syndey hall' }

I'll eventually want to remove duplicate names and display in a list like so... (But that's down the line. For now I just want to grab matching objects).

Id: 1    Name: Cal    Location: McHale
                                Syndey Hall

I've tried:

const result = _.find(students, {student_id: studentId});

But that doesn't seem to work, it just returns one of the objects with that id..

{id: 1, name: 'Cal', location: 'McHale' },

How can I make this work?

Modelesq
  • 5,192
  • 20
  • 61
  • 88
  • 2
    https://lodash.com/docs#filter - that said, given that it's an array you're filtering, not an object, you don't even need Lodash in this case. You can just use the built in [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) function. – Joe Clay Jun 16 '16 at 15:53
  • 1
    @JoeClay Huh, lodash filter seems easier. Is there a benefit to using . Array.prototype.filter() `students.filter(x => x.student_id === studentId);` ? – Modelesq Jun 16 '16 at 16:01
  • @Modelesq the built in one is probably faster because the lowdash one is doing a lot more stuff: https://github.com/lodash/lodash/blob/4.13.1/lodash.js#L8458 – JustGage Jun 16 '16 at 16:29
  • 1
    @JustGage word. thanks for the reply :D – Modelesq Jun 16 '16 at 16:36
  • 1
    Yep, @JustGage is right - in general I'd favor built-in methods over Lodash's where possible, as they tend to be implemented in native code rather than JavaScript, which usually ends up being faster. That said, unless this is taking place in a really performance-critical part of your code, it probably won't make a significant difference! – Joe Clay Jun 17 '16 at 08:15
  • Does this answer your question? [Return all matching elements of an array of objects?](https://stackoverflow.com/questions/26844260/return-all-matching-elements-of-an-array-of-objects) – Heretic Monkey May 25 '21 at 19:48

4 Answers4

24

I would look into the filter function. It's build into JavaScript.

Here's an example of how it works. All you need to do is find a way to make a function that will tell if it has the proper id.

function isBigEnough(value) {
  return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
JustGage
  • 1,534
  • 17
  • 20
9

If you see the documentation for _.find it states

Iterates over elements of collection, returning the first element predicate returns truthy for.


You should use the _.filter method for what you want

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

Something like

const result = _.filter(students, {student_id: studentId});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
2
const result = students.filter(e => e.id === 1);
rahul
  • 21
  • 3
1

Find will always return the first matching element, regardless of how many other elements may match your condition.

If you want to extract all matching elements, you will need to use the .filter method.

The implementation is the same, but the result varies from an object (.find()) to an array (.filter())

Kvz
  • 1,141
  • 1
  • 3
  • 9