我是 javascript 新手,所以我有一个包含三个数组的对象,我想映射它并列出我的数据 const [Foods,setFoods] = useState({ "id_food":[1,2,3], "name_food":["food1","food2","food3"], "quantity":[10,22,12] })一个虚构的代码:{ Foods.map((id,name,qte) =>( <p>{id}{name}{qte}</p> ))}
3 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
Object.keys(Foods).forEach(prop => { Foods[prop].map((item) => console.log(item)); });
德玛西亚99
TA贡献1770条经验 获得超3个赞
也许是这样的:
Foods.id_food.map((id, i) => {
const name_food = Foods.name_food[i];
const quantity = Foods.quantity[i];
return <AwesomeReactComponent key={id} nameFood={name_food} quantity={quantity}/>;
});
慕雪6442864
TA贡献1812条经验 获得超5个赞
let mappedArray = Foods.id_food.map((ele, i) => {
let name = Foods.name_food[i]
let quantity = Foods.quantity[i]
return {id: ele, name: name, quantity: quantity}
})
输出:
(3) [{…}, {…}, {…}]
0: {id: 1, name: "food1", quantity: 10}
1: {id: 2, name: "food2", quantity: 22}
2: {id: 3, name: "food3", quantity: 12}
添加回答
举报
0/150
提交
取消