I want to find a better, more elegant solution than using 2 iteration to find a value in an object. I looked at functions to search within an array such as find,indexOf but was wondering I could search through the object without using a for or forEach.
Let say I want to find all swimmers. What I got so far.
members = [
{ name: "Sue", hobby: ["Swimming", "Running", "Dancing"] },
{ name: "Sam", hobby: ["Cars", "Travelling"] },
{ name: "John", hobby: ["Reading", "Swimming"] },
{ name: "Rob", hobby: ["Running", "Coding"] },
];
function findSwimmers(members, hobby) {
let swimmers = [];
members.forEach(function (e){
e.hobby.forEach(function (element){
if (element.toLowerCase() === hobby.toLowerCase()) {
swimmers.push(e);
}
});
});
return swimmers;
}