So I have 3 API sources that return data in this format
firstSource = [{month, endDate, expectedPrice}] ->array of 12 objects
secondSource = [{month, value}] ->array of 12 objects
thirdSource = [{month, value}] ->array of 12 objects
example:
secondSource = [
{month: 1, value: 234.22},
{month: 2, value: 123.58},
{month: 3, value: 255.35},
...
...
you get the idea
]
I’ve come across this solution that uses lodash library. This is all well, but I’m interested in a solution that doesn’t require external libraries. So basically, I want to achieve the same thing the person from the lodash solution did -> combine all three data sources into one and then use that to display the data in <table>
element, only without using external libraries, e.g. lodash.
Expected output:
combined = [
{month, endDate, expectedPrice, secondSourceValue, thirdSourceValue}
]
Any ideas?
2
Answers
We can use
array.map
to merge multiple arrays of objects and create a new array with properties of all objects.Do note that, in this case, the length of all the arrays should be the same.
what’s the problem to try something like this?
You only have to check if the second and third sources have the same length with the main.