0

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?

6
  • Looks like you need to post more code. Where are you accessing a field called should? Commented Feb 18, 2018 at 23:08
  • Array.filter returns an array, more info so findPerson is an array, and to get the person you may want to const person = findPerson[0], and also, I believe you're going to want to return person when name matches, like so: if(person.name === name) { return person; }; Commented Feb 18, 2018 at 23:08
  • Should is just the error that generates whenever I run the test to see if my code passes the test. And I don't want to hardcode an index value, essentially, I want to pass a name to the callback function and the name that matches that, to essentially produce the object containing the person's name and gender Commented Feb 18, 2018 at 23:10
  • I'm confused how its undefined, as I didn't think I would have to create and empty object to push the object to since filter is already understood as doing that from what I've read... Commented Feb 18, 2018 at 23:11
  • Are you looking to get back an array of people, or just the first one it finds? For example, if there were multiple people named "Erika", do you want all of them or just the first one? Commented Feb 18, 2018 at 23:18

2 Answers 2

1

filter function should return true or false:

var people = [
  {name: 'Max', gender: 'Trans'},
  {name: 'Sue', gender: 'Female'},
  {name: 'Jake', gender: 'Male'},
  {name: 'John', gender: 'Male'},
  {name: 'Erika', gender: 'Female'}
];

const findPerson = (people, find) => people.filter(({name}) => name === find)[0];

console.log(findPerson(people, 'Erika'))

Sign up to request clarification or add additional context in comments.

5 Comments

Why did you put an index of 0 at the end?
filter method returns an array, so I've returned the first element. You can omit it if you want to get all matches as an array.
Ok I understand that, thank you. But now my second question, if I just want to return it as an object, instead of an array, should I construct a new empty object? Because now I'm getting an object inside of an array, and I just want to return the object
My solution returns object.
scratch that I got it this time. Thank you very very much!
0

const people = [{
    name: 'Max',
    gender: 'Trans'
  },
  {
    name: 'Sue',
    gender: 'Female'
  },
  {
    name: 'Jake',
    gender: 'Male'
  },
  {
    name: 'John',
    gender: 'Male'
  },
  {
    name: 'Erika',
    gender: 'Female'
  }
];

const findPerson = (persons, name) => {
  return persons.filter((person) => person.name === name);
};

console.log(findPerson(people, 'Erika')[0]);

The issue with your code is that you were not returning the value of calling filter.
filter after it has run, returns an array. It's that returned array that will contain the value that was filtered out.

I did this: findPerson(people, 'Erika')[0] to select the first item from the array since the return value of filter is an array.

3 Comments

So that solves problem number one for me, thank you for pointing that out. But the problem with this code now is that Its returning it as an array, instead of an object: should return an object ‣ AssertionError: expected [ { name: 'Erika', gender: 'Female' } ] to be an object should return the proper object ‣ AssertionError: expected [ { name: 'Erika', gender: 'Female' } ] to deeply equal { name: 'Erika', gender: 'Female' }
filter cannot return an object. Either you change your assertion or you pick the first item in the array i.e. findPerson(people, 'Erika')[0].
Nail on the head. Thank you very very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.