Help with array methods!

Hey guys,

Im doing some random javascript challenges and got super stuck with array methods.
In this pen right here: https://codepen.io/jimmy1992/pen/bGrRQrd
I am supposed to filter out users who are dog owners, so basically those that have hasDog: true. But how do I do that? I tried applying the filter method, but just can’t seem to figure out how to apply it:(

Same goes for task number two, I think I’m supposed to filter through an array and return another array with users who are 18+ yrs?

Please help! Thanks in advance.

Hey @Nefro I can’t see your attempt at using filter in the pen?

Have you gone through this exercise? Seems very applicable.

1 Like

Hey, thank you for you response!

No, I’m still struggling. I deleted my attempt at it, thought it would be clearer just to post the plain task.

It is always helpful to post the code you’ve tried. It helps others see what specifically is tripping you up.

I agree that some practice lessons on array methods might be useful if you haven’t used them before.

2 Likes

No worries.

Good intentions I can tell :slight_smile:

JeremyLT makes a good point. That help button is a life saver.

1 Like

Hey guys!

Thanks to your example I was able to do part of the solution.

const filterDogOwners = users.filter (user => user.hasDog === true);
console.log(filterDogOwners);

But how do I turn it into a function and make it dynamic? I tried it many ways but I keep getting error messages in the console, I’m most likely messing up the syntax somewhere.

EDIT: I figured part one out!

function filterDogOwners (array) {

const hasDog = array.filter(owner => owner.hasDog === true);

console.log(hasDog);

}

Good job! Remember that functions should always return something if you want to have more use out of it