Skip to content
Advertisement

API return values based on certain criteria

I am working on an API, and I want to return values based on some input I receive. For example, here is a body sample input

JavaScript

Based on the fields array, I want my API to return only those values. Here is an example of the API implementation

JavaScript

So the part where I indicate stuck here I want to loop through the result array of objects and return only the properties that match the values gotten from the field’s array.

Here is a typical API Response

JavaScript

So my final API Response should be

JavaScript

How do I go about this please.

Thank you

Advertisement

Answer

JavaScript

Just doing a .map() over your response and inside that map just iterate over your fields and returned the mapped result based on those fields.

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