Skip to content
Advertisement

How can I create new array from this JSON response?

I am really struggling to create a new array from this response, I don’t know where and how to start or to do it:

JavaScript

I want to get this new structure/array:

JavaScript

How can I make for each fruit to add all sizes as subarray from the sizeAndFruit as the example above? After that each fruit has to be row in a table, that’s why I need to get this new array? Please let me know if you have any suggestions or any extra questions I will be glad to explain more if needed. Thank you very much!


Edit for David: How about if we always order them by size? Here is the snippet: pastecode.io/s/t82n19x4 so when the order is like this. So, to order them by size as it is now on David’s reply, because if these are mixed then they order as they go instead by 100-500 5001-10000 10,000+ they go 10.000+ 100-500 5001-1000, etc. Thank you!

Advertisement

Answer

The below codes reads your inputs and turns it into your desired output (including sorting by size):

JavaScript

Prints as:

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