0

I've tried to figure this out with another similar posts, but can't find the right way to do this:

i have this array:

let concessions = [
     {
        name: Auto Cars,
        brands: ['Ferrari', 'Seat', 'Kia']
     },
     {
        name: ParisCars,
        brands: ['Opel', 'Ford', 'Honda']
     },
]

and i have this another array:

let wantedCars = ['Ferrari', 'Toyota'];

I need to filter each object from the concessions array and create another array only with concessions that includes one or more brands that existes on wantedCars array. I've tried this:

let filteredConcessions = concessions.filter(concession => concessions.brands.includes(wantedCars))

but, return 0.

Any advice?

1
  • Same can be achieved using .find like ``` concessions. filter(concession => concession.brands.find(b => wantedCars.includes(b))); ``` Commented Sep 10, 2020 at 18:13

1 Answer 1

2

You should use the function Array.prototype.some instead because you need to compare a property and not the whole object.

let filteredConcessions = concessions.
            filter(concession => concession.brands.some(b => wantedCars.includes(b)));

let concessions = [   {      name: "Auto Cars",      brands: ['Ferrari', 'Seat', 'Kia']   },   {      name: "ParisCars",      brands: ['Opel', 'Ford', 'Honda']   }],
    wantedCars = ['Ferrari', 'Toyota'],
    filteredConcessions = concessions.
            filter(concession => concession.brands.some(b => wantedCars.includes(b)));
            
console.log(filteredConcessions);

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

Comments

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.