I have a nested JSON and want to end up with the result array, how is this possible? Any tips on how i can accomplish this? Do i need to use a nester for in/of loop? Higher order functions etc? I am new to nested objects, any tip or good reference will be appreaciated!
// const db shows the desired result once db.save() is called. I want it to include the Date, Symbols (USD etc.) and the value of it - all wrapped inside their own object
const db = [
{ Date: '1999-01-05', AUD: 1.8944, SEK: 9.4025, USD: 1.179 },
{ Date: '1999-01-06', AUD: 1.882, SEK: 9.305, USD: 1.1743 },
];
// the json that i recieve upon fetching
const json = {
rates: {
'1999-01-08': {
AUD: 1.8406,
SEK: 9.165,
USD: 1.1659,
},
'1999-01-06': {
AUD: 1.882,
SEK: 9.305,
USD: 1.1743,
},
'1999-01-07': {
AUD: 1.8474,
SEK: 9.18,
USD: 1.1632,
},
'1999-01-05': {
AUD: 1.8944,
SEK: 9.4025,
USD: 1.179,
},
},
start_at: '1999-01-05',
base: 'EUR',
end_at: '1999-01-10',
};
Here's a solution using nested
for
loops and Object.entires to get a key/value pair from your object. It creates an array of objects just like your desired result. (Not in the exact order, as Javascript doesn't care for keeping objects sorted).Usage; in your posted example, would be: