+ 14

[SOLVED] Map a specific json into a list which contains a list with every property plus a new property?

What i have is this: { "rates": { "2018-01-02": { "CAD": 1.5128, "HKD": 9.4283, }, "2018-01-03": { "CAD": 1.5047, "HKD": 9.3985, }, "2018-01-04": { "CAD": 1.5068, "HKD": 9.4188, }, } } And I want it to look like this: [ {"CAD": 1.5128, date: "2018-01-02"}, {"HKD": 9.4283, date: "2018-01-02"}, {"CAD": 1.5047, date: "2018-01-03"}, {"HKD": 9.3985, date: "2018-01-03"}, {"CAD": 1.5068, date: "2018-01-04"}, {"HKD": 9.4188, date: "2018-01-04"} ] I can't find a way to map this to look like above, because of the nesting. I can work without the date, if i get only the simple values of currencies because all i have is the "rates" and currencies object, without any date nested: const data = json.rates; {Object.entries(data).map(([key, value], index) => <tr key={index}> <td>{key}</td> <td>{value}</td> </tr>)} But for mu

18th May 2020, 2:38 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
3 Respuestas
18th May 2020, 3:02 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
Ore Adeleye you're right about that 👍, it would've been much easier that way. but the problem is the json looks like this: "rates": { "2018-01-02": { "CAD": 1.5128, "HKD": 9.4283, }, "2018-01-03": { "CAD": 1.5047, "HKD": 9.3985, }, "2018-01-04": { "CAD": 1.5068, "HKD": 9.4188, }, } and i have like 30 types of currency values, and the dates can be up to 2 years. thank you for your solution though! i keep that in mind since it's an easier way to solve a problem like that one 👍
19th May 2020, 10:14 AM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 3
👑 Prometheus 🇸🇬 answer works but just so you know, you can also use array.reduce method on JSON type arrays. https://code.sololearn.com/W45d0xG1Ebnb/?ref=app
19th May 2020, 10:03 AM
Ore
Ore - avatar