【发布时间】:2021-01-26 03:52:18
【问题描述】:
我正在尝试将购物车添加到我的应用程序中。我需要通过将 ID 与我的 cart 数组 中的对象匹配来过滤我的 product 数组,然后将其显示在我的 Flatlist 上。 我已经测试以确保我可以使用其他方法(如 include())过滤产品列表。
我无法让当前代码工作。
为了便于阅读,我已将代码简化到最低限度。
<View>
<Text>
Cart
</Text>
<FlatList
data={this.props.products.products.filter(product => this.props.cart.cart.some(el => el === product.id))}
renderItem={renderMenuItem}
keyExtractor={item => item.id.toString()}
/>
</View>
这是我保存数组的 db.json 文件。
"products": [
{
"id": 0,
"name": "Ezywhip Pro",
"category": "chargers",
"label": "",
"featured": false,
"description": "Ezywhip Pro Cream Chargers, Made by MOSA",
"image": "images/ezywhip.png",
"quantity": 0,
"price": 0
},
{
"id": 1,
"name": "Ezywhip Pro",
"category": "ezy",
"label": "",
"featured": false,
"description": "Ezywhip Pro Cream Chargers, Made by MOSA",
"image": "images/ezywhip.png",
"quantity": 50,
"price": 40
},
{
"id": 2,
"name": "Ezywhip Pro",
"category": "ezy",
"label": "",
"featured": false,
"description": "Ezywhip Pro Cream Chargers, Made by MOSA",
"image": "images/ezywhip.png",
"quantity": 100,
"price": 70
}
],
"cart": [
{
"id": 1
}
]
结果当前是一个空平面列表。 提前感谢您的任何反馈。
【问题讨论】:
-
你的 this.props.cart 是什么样的?
-
如果您的购物车商品是一个对象,我认为您缺少对象属性,例如 el.id
标签: reactjs react-native react-redux shopping-cart