Question:
I have an array of object from which I am trying to get values using map operator but I am getting the whole json objects all I want is just array of values. Below is my code:Answer:
You can do this recursively. You can first start off by grabbing the values of your object, and then loop through those using.flatMap()
. If you encounter a value that is an object, you can recursively grab the values of that object by recalling your function. Otherwise, you can return the value. The advantage of using .flatMap()
here is that when the recursive call returns an array, we don’t end up with inner arrays, but rather the array gets flattened into one resulting array:If you have better answer, please add a comment about this, thank you!