Cannot read properties of undefined (reading 'map')
时间: 2023-09-24 18:05:16 浏览: 101
当你在前端获取对象集合中的内容时,如果该对象为空或未定义,就会出现"Cannot read properties of undefined (reading 'map')"的报错。这个错误是因为你在尝试对一个未定义的对象进行map操作,而map只能用于数组类型的对象。为了解决这个问题,你可以在使用map之前进行判断对象是否存在。有两种常用的方法可以做到这一点:
1. 可选链式操作符:使用可选链式操作符`?.`来判断对象是否存在,如果对象存在,则执行map操作。示例代码如下:
```javascript
this.array?.map(() => { ... })
```
2. 使用逻辑与(&&)判断:使用逻辑与操作符`&&`来判断对象是否存在,如果对象存在,则执行map操作。示例代码如下:
```javascript
this.array && this.array.map(() => { ... })
```
通过以上两种方法,你可以在使用map操作之前进行判断,避免出现"Cannot read properties of undefined (reading 'map')"的报错。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [TypeError: Cannot read properties of undefined (reading ‘map‘)](https://blog.csdn.net/WX_nbclass/article/details/128711850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Cannot read properties of undefined (reading ‘map‘)](https://blog.csdn.net/qq_64360391/article/details/127432349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)