Why is the Lodash _.find method returning undefined?

10,858

It is working. The console.log might be confusing you:

var rooms = [
  { title: 'Just For Fun', created: '2016-10-23T16:57:03.288Z', id: 2 },
  { title: 'Development', created: '2016-10-23T16:57:03.294Z', id: 6 }
];
var room = _.find(rooms, {'id': 6});
console.log(room); // Object {title: "Development", created: "2016-10-23T16:57:03.294Z", id: 6}
Share:
10,858
Craig van Tonder
Author by

Craig van Tonder

Updated on June 14, 2022

Comments

  • Craig van Tonder
    Craig van Tonder almost 2 years

    I have a collection and using the Lodash _.find method, I want to return the object with the title matching "Development".

    So I have the following code which I had hoped would return what I wanted:

    // Define rooms
    var rooms = [
      { title: 'Just For Fun', created: '2016-10-23T16:57:03.288Z', id: 2 },
      { title: 'Development', created: '2016-10-23T16:57:03.294Z', id: 6 }
    ];
    // Load lodash module
    var _ = require('lodash');
    // Expected object for development?
    console.log(_.find(rooms, {'id': 6}));
    

    However, what I get back in the console is simply undefined. The documentation has the following example:

    var users = [
      { 'user': 'barney',  'age': 36, 'active': true },
      { 'user': 'fred',    'age': 40, 'active': false },
      { 'user': 'pebbles', 'age': 1,  'active': true }
    ];
    // The `_.matches` iteratee shorthand.
    _.find(users, { 'age': 1, 'active': true });
    // => object for 'pebbles'
    

    So they get pebbles but I get undefined? Can anyone indicate where I am going wrong here? Thanks in advance!

    I am using Node.