I am a Reactjs noob and have an object with the structure below. In my functional component I would like to use the most modern approach to iterating through the keys at the top level in the object hierarchy (i.e. A, B, C) and display the value associated with the 'name' keys.
data.json
{
"A": {
"name": "AA",
"type": "AB",
"values": [
["AC", "AD", "AE"],
["AF", "AG", "AH"]
]
},
"B": {
"name": "BA",
"type": "BB",
"values": [
["BC", "BD", "BE"],
["BF", "BG", "BH"]
]
},
"C": {
"name": "CA",
"type": "CB",
"values": [
["CC", "CD", "CE"],
["CF", "CG", "CH"]
]
}
}
My desired result is to display the following on screen:
A - AA
B - BA
C - CA
I have attempted to do this using the map function, but I later read that map only works with arrays, not objects? I've also tried this answer: https://stackoverflow.com/a/14810722/679841 but conscious that (a) I wasn't able to get it to work in my case, perhaps because my object is a little bit more complex and I'm new to this, and (b) there have been a few updates which makes me wonder whether there is an even better approach now?
Here is a simplified veresion of my functional component.
FunctionalComponent.js
import React from 'react';
import Data from './data.json'; //See above for content of this json file
const data= Data; //not sure if this is necessary?!
function Loop(){
<form>
return(
//This is where I want to start my loop and dynamically populate
<div>{data.A.name} - {data.A.name}</div>
//This is where I want to end the loop
);
</form>
}
export default Loop;
Any help to achieve my desired result would be much appreciated.
Many thanks,
Katie