我是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>
))}
答案 0 :(得分:1)
Object.keys(Foods).forEach(prop => {
Foods[prop].map((item) => console.log(item));
});
答案 1 :(得分:1)
也许是这样的:
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}/>;
});
答案 2 :(得分:0)
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}