Skip to content
Advertisement

How to filer an array of objects and return only one object based on a specific value

I have an array containing two plan objects:

JavaScript

What I am trying to do is use a value (customerPlanId) to find the correct plan by matching it to the plan.id and then just return the correct plan, like below:

JavaScript

I know I can map through the plans and filter for the correct plan, but then how can I return the correct plan object on its own?

Advertisement

Answer

Solution

You could use the “Array.find” method

Example

Stackblitz Snippet

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement