"TypeError: Cannot read properties of undefined (reading 'map')"
出现类似的情况,调用数组方法,报识别不到的错误,是因为你要更改的数组为空,所以是是别不到此方法。
选则商品是渲染已选择的商品
checkProductAttrDesc(){
if(this.specificationList.length){
return this.checkAllId.map((item,index)=>{
return this.specificationList[index].valueList.find(val=>val.id===item).value
}).join(',')
}else{
return ''
}
},
checkProductImsge(){
if(this.specificationList.length){
const checkAttrList= this.checkAllId.map((item,index)=>{
return this.specificationList[index].valueList.find(val=>val.id===item).pic_url
})
let picUrl=''
checkAttrList.forEach(item=>{
if(item) picUrl=item
})
return picUrl
}else{
return this.info.list_pic_url
}
},
先判断商品是否有多种规格,如果有当我们选择时,保留选择的商品对应的索引,之后在遍历索引,拿出来放入数组中,list[0],这种形式。遍历次商品中的类型中的索引和我们选中的索引相同我们就返回,我们就可以索取所选取的商品信息了。
添加购物车
这篇博客讨论了在JavaScript编程中遇到的`TypeError: Cannot read properties of undefined (reading 'map')`错误,该错误通常发生在尝试对空数组调用方法时。作者提供了检查商品规格和选择的商品信息的代码示例,强调在操作数组前应确保其非空,以避免这类错误。同时,代码展示了如何根据商品选择渲染已选择的商品,并在添加购物车时处理不同规格商品的信息。

被折叠的 条评论
为什么被折叠?



