Skip to content
Advertisement

Dynamically get values of object from array

Let’s say I have an Object myBook and an array allCategories.

JavaScript

What I want : Loop over categories to check the value of Book, for example, check if "sciencefiction" exists in my Book Object and then check it’s value

What I have tried :

1) With indexOf

JavaScript

2) With includes

JavaScript

Expected output :

JavaScript

If you need more information, just let me know, I’ll edit my question.

Advertisement

Answer

You could use filter and find methods to return new array of categories and then use map method to return array of elements.

JavaScript

You can also use reduce instead of filter and map and endsWith method.

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