Given an array of objects, I am trying to write a method that can get the index of an element where a specific property has a value that happened ntimes in the array.
What I'm trying to achieve is perhaps easier to describe with this code:
var foods = [
{
name: "orange",
owner: "bob"
},
{
name: "carrot",
owner: "fred"
},
{
name: "apple",
owner: "bob"
},
{
name: "onion",
owner: "fred"
},
{
name: "banana",
owner: "bob"
},
{
name: "pear",
owner: "bob"
}
];
function getIndex(owner, nthItem){
}
getIndex("bob", 3);
I would prefer a well-formed underscore / lodash solution over a 20-bit pure JS single-line. If you can do it less than with pure JS then this is a great toc.
I tried some things with _.groupBy and _.pluck to get separate lists, but cannot find a way to translate this information back to the index from the original array.