简体   繁体   English

为什么Lodash _.find方法返回未定义?

[英]Why is the Lodash _.find method returning undefined?

I have a collection and using the Lodash _.find method, I want to return the object with the title matching "Development". 我有一个集合,并使用Lodash _.find方法,我想返回标题与“ 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 . 但是,我在控制台中得到的只是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 ? 所以他们得到pebbles但我undefined Can anyone indicate where I am going wrong here? 谁能指出我在哪里出问题了? Thanks in advance! 提前致谢!

I am using Node. 我正在使用Node。

It is working. 这是工作。 The console.log might be confusing you: console.log可能会使您感到困惑:

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM