javascript - 在 ReactJS 的表格行中显示数组数据
问题描述
我怎样才能productName
在里面输出return
呢?来自选定items
的购物车项目。( function) 确实显示在控制台中,但我无法让它显示在渲染的返回中。const order
const mapState = ({ user }) => ({
currentUser: user.currentUser,
});
const mapCartItems = createStructuredSelector({
items: selectCartItems,
});
const CheckingOut = (product) => {
const { total, cartCount, items } = useSelector(mapCartItems);
const order = {
orderItems: items.map((item) => {
const { productName, qty} = item;
return {
productName,
};
console.log(item.productName); // this one is showing in the console
}),
};
return (
<div>
<Container fixed>
<table >
<tr>
<td>Name : </td> // like show the items here
<td></td>
</tr>
<br></br>
<tr>
<td>
<PaymentIcon
style={{ marginRight: "1rem", color: " #e31837" }}
/>
Total Price:
</td>
<td>₱{total}.00</td>
</tr>
</table>
//just other codes for submit
</Container>
</div>
);
};
export default CheckingOut;
解决方案
const order = (product) => {
const order = {
orderItems: items.map((item) => {
const { productName, qty} = item;
return {
productName,
};
}),
};
order.orderItems.forEach((item) => console.log(item.productName)) // will log all productName
return order.orderItems
}
函数order
将返回order.orderItems
具有对象数组的函数。例如:
order :{
orderItems: [
{
productName: 'car',
},
{
productName: 'bike',
}
]
}
要访问每个产品名称,您必须遍历嵌套在order.orderItems
order(product).forEach((item) => {
console.log(item.productName) // car, bike
});
推荐阅读
- r - 如何根据另一列中的索引操作一列中的子字符串
- javascript - 尽管有适当的 Access-Control-Allow-* 标头,但 CORS ajax 调用失败
- excel - Excel 2016 散点图 - 如果带有公式的单元格为空白,则 x 轴上的位置错误
- elasticsearch - Logstash Elasticsearch 输出给出 401 错误
- unauthorized - Ant 媒体服务器在获取 VOD 块时使令牌无效
- amazon-web-services - 限制单个 AWS Lambda 的 AWS Secrect
- java - 按 Java List 中的第一个元素过滤
- list - 在 Robot Framework ***Variables*** 部分中将列表变量初始化为空
- javascript - 在电子表格的列中打印嵌套数组
- python - Pulumi Import GCP Instance Error in boot_disk 属性冲突