Просто объедините _.find
с _.has
.
Вы даже можете создать миксин (он же плагин), как показано ниже.
Чистая версия JS не так уж и плоха.
_.mixin({
findObjectByPresentKey : function(arr, key) {
return _.find(arr, function(obj) {
return _.has(obj, key);
})
}
});
const myArray = [
{ uKey1: 'hey', otherValue: 1 },
{ uKey2: 'hey', otherValue: 1 },
{ uKey3: 'hey', otherValue: 1 }
]
// In-line
console.log(_.find(myArray, 'uKey3'))
// As a plugin
console.log(_.findObjectByPresentKey(myArray, 'uKey3'))
// Pure JS (Edge)
console.log(myArray.find(obj => obj.hasOwnProperty('uKey3')));
// IE 9+
console.log(myArray.filter(obj => obj.hasOwnProperty('uKey3')).pop());
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Пожалуйста, обратитесь к: _.find(collection, [predicate=_.identity], [fromIndex=0])
// The `_.property` iteratee shorthand.
_.find(users, 'active');
// => object for 'barney'