1 回答
TA贡献1719条经验 获得超6个赞
你考虑过字典吗?
const data =
{
'W1':{ color: "red" , personId: 'P77' }
,'W7':{ color: "yellow", personId: 'P21'},
}
const persons =
{
'P77':{ name: 'Peter', favoriteFoodId: 'FF4' },
'P21':{ name: 'John', favoriteFoodId: 'FF9' }
}
const favoriteFoods =
{
'FF9':{ food: 'pasta', description: 'fresh italian pasta from stone oven' }
,'FF4':{ food: 'banana', description: 'fresh bananas from the tree' }
}
function getFavFood( pId , field ){
return favoriteFoods[ persons[ data[pId].personId ].favoriteFoodId ][field]
}
// not sure what template library you'll be using
// but for now plain old js....
table = "<TABLE>"
for( d in data){
table += `<TR style="background:${data[d].color}">`
+ `<TD>${ persons[ data[d].personId ].name }</TD>`
+ `<TD>${ getFavFood( d , "food" ) }</TD>`
+ `<TD>${ getFavFood( d , "description" ) }</TD>`
+`</TR>`
}
table += "</TABLE>"
document.body.insertAdjacentHTML( "beforeend" , table )
添加回答
举报