How about using _.find(collection, [predicate=_.identity], [fromIndex=0])
of lo-dash to get object from array of objects by object property value. You could do something like this:
var o = _.find(jsObjects, {'b': 6});
Arguments:
collection (Array|Object): The collection to inspect.
[predicate=_.identity] (Function): The function invoked per iteration.
[fromIndex=0] (number): The index to search from.
Returns
(*): Returns the matched element (in your case, {a: 5, b: 6}), else undefined.
In terms of performance, _.find()
is faster as it only pulls the first object with property {'b': 6}
, on the other hand, if suppose your array contains multiple objects with matching set of properties (key:value), then you should consider using _.filter()
method. So in your case, as your array has a single object with this property, I would use _.find()
.