I have severel Objects containing one sort of data: Prices:
'btc-usd' : 2640, 'ltc-usd': 40, ...
Amount of Crypto:
'btc-usd': 2.533, 'ltc-usd': 10.42, ...
How can I take these Objects and create an Array of Objects like:
[ { name: 'Bitcoin', amount: 2.533, value: 2640, id: 'btc-usd' },
{ name: 'Litecoin', amount: 10.42, value: 40, id: 'ltc-usd' }, ...
]
Thanks a lot for your help!
First, create a dictionary of what each abbreviation stands for.
Then, populate an empty array with objects containing the relevant information. In each of these objects, the name would correspond to the relevant key within the
dictionary
object. At the same time, the amount and value would correspond to the relevant key within theamounts
andprices
objects respectively. Finally, theId
would correspond to thekey
itself.You could use a hash map (e.g. 'btc-usd' => {name:"Bitcoin",...}) to create new objects. This hashmap can be easily converted to an array.
http://jsbin.com/fadapafaca/edit?console
Here's a generalized function,
add
, that accepts a field name and an object of values and maps them into aresult
object which can then be mapped into an array.You could map the keys of one of the objects to produce a new array of objects. You just have to make sure, that the key is in every of these objects.