下划线_.find当它存在时,没有find数组中的元素

我试图使用Underscore.js libarary的findWhere函数有效地从node.js服务器上的数组中find一个对象,我不明白为什么它总是返回undefined。

我使用节点控制台testing了该函数,并且该对象definetely包含具有指定键的对象,但在使用findWhere时仍然不返回任何内容。

在这里,我确保我正在查找的“夹具”键的值确实等于我希望返回的对象中的匹配值:

'55785f4e38bd12511018145d'==预测[0] .fixture;

真正

当检查在查找的数组中保留的值是什么时,我确认该值是否存在:

预测

[{fixture:'55785f4e38bd12511018145d'}]

检查我希望返回的值:

预测[0] .fixture

'55785f4e38bd12511018145d'

运行findWhere查询返回undefined:

var foundPrediction = underscore.findWhere(predictions,{fixture:'55785f4e38bd12511018145d'});

未定义

我能想到的唯一原因是,这可能与铸造或可能是一个'==='在函数中返回false,但有什么办法,我可以findWhere按照需要返回对象,或将我不得不求助于使用for循环的手动search?

提前致谢!

你的代码没有错。 看起来你是在控制台上运行,这就是为什么在敲回车后,控制台显示未定义。 你也许认为,发现预测值是未定义的。 您需要安慰foundPredictionvariables来获得所需的结果。

也没有任何错误的铸造,因为它返回true。

var predictions = [{ fixture:'55785f4e38bd12511018145d' }] console.log('55785f4e38bd12511018145d' === predictions[0].fixture); var foundPrediction = _.findWhere(predictions, {fixture: '55785f4e38bd12511018145d'}); console.log(foundPrediction); 

我已经在jsFiddle中添加了代码。 希望能帮助到你