From the documentation, we have _.findIndex
.
findIndex _.findIndex(array, predicate, [context])
Similar to .indexOf, returns the first index where the predicate truth test passes; otherwise returns -1.
Once you have the index of the item in question, you can splice
the array at that index and retrieve the 0th index item from the length-1 array returned from Array.prototype.splice
.
var sourceArray = [
{name:'java'},
{name:'ruby'},
{name:'javascript'},
{name:'meteor'}
];
var index = _.findIndex(sourceArray, function(x) {
return x.name === 'javascript';
});
var item = sourceArray.splice(index, 1)[0];
EDIT: You could also do it with _.findWhere
and _.without
as shown in mef's answer. It's a bit more inefficient, but also a bit more readable. However, note that it will remove all instances of an object with that key-value pair instead of just the first, so make sure that's the behavior you want.