To give background of the prompt (this isn't homework, but some questions that someone forwarded me to help with understanding how to use HOF and implementing them correctly so all explanations as well as different approaches to the problem are welcomed):
a) Implement a findPerson method that takes an Array of people and a name String as the target. Each person Object is structred:
{name: 'Erika', gender: 'Female'}
usage example:
findPerson(people, 'Erika') // -> {name: 'Erika', gender: 'Female'}
Constraint: Use filter
My array of objects is as follows:
var people = [
{
name: 'Max',
gender: 'Trans'
},
{
name: 'Sue',
gender: 'Female'
},
{
name: 'Jake',
gender: 'Male'
},
{
name: 'John',
gender: 'Male'
},
{
name: 'Erika',
gender: 'Female'
}
];
The code that I have constructed thusfar is this:
const findPerson = (people, name) => {
people.filter(function(person) {
if(person.name === name){}
return person;
});
};
The problem is that I am running into this error as follows: should return an object ‣TypeError: Cannot read property 'should' of undefined
should return the proper object ‣TypeError: Cannot read property 'should' of undefined
If anyone could be of assistance of pointing me in the right direction as to how to go about my logic of solving this and where did I go wrong in my code?
should?Array.filterreturns an array, more info sofindPersonis an array, and to get the person you may want toconst person = findPerson[0], and also, I believe you're going to want to returnpersonwhen name matches, like so:if(person.name === name) { return person; };