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.
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.
Share Improve this question edited Oct 23, 2016 at 17:33 user663031 asked Oct 23, 2016 at 17:18 Craig van TonderCraig van Tonder 7,68718 gold badges65 silver badges111 bronze badges 4-
(1) Can you show your code in one code block, so the order is clear? Have you defined
room
before doing the_.find
? (2) how have you installed lodash? (3) which version of Node are you running on? – trincot Commented Oct 23, 2016 at 17:24 - @trincot I see what you're getting at but I was indeed defining rooms before the console.log(). Have updated my question. – Craig van Tonder Commented Oct 23, 2016 at 17:30
-
1
You know of course that
console.log
itself returns undefined so that will be output to the console. The line above that should be the element it found. Usually screen shots are a bad idea but in this case it might be useful. – user663031 Commented Oct 23, 2016 at 17:31 - 1 @torazaburo The proof is in the pudding, was going to make a screen shot when I realized that I had encapsulated the id as a string in my test and in the example here on SO it is an integer. That is the problem, thanks for your help! ;/ – Craig van Tonder Commented Oct 23, 2016 at 17:44
1 Answer
Reset to default 3It 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}