I have an object:
{
type : "consultations/get",
payload : [
{name : "bob",
favoriteColor : "blue",
age : 54},
{name : "steve"
favoriteColor : "red"
age : 45},
{name : "sarah",
favoriteColor : "green",
age : 38}
]
}
How can I take name and favoriteColor key and value and map to a html table? I do not want to display age and I would like to be able to set which rows (payload keys) are shown (i.e favoriteColor and name) and have the corresponding values mapped to a table cell. Any help would be appreciated. Thank you in advance. Here is the approach I have taken thus far:
var json = {
type : "consultations/get",
payload : [
{name : "bob",
favoriteColor : "blue",
age : 54},
{name : "steve",
favoriteColor : "red",
age : 45},
{name : "sarah",
favoriteColor : "green",
age : 38}
]
};
var tableHeader = `
${Object.keys(json.payload[0]).filter(e => e !== "age").map((row) => `
<th>${row}</th>`).join('')}`;
The tableHeader comes out fine, I'm just stuck on how I can filter and map the data. After making the tableHeader would I just loop through the payload and add the td's accordingly? How can I do this properly with the particular filter I have placed on the keys?