arrays - React - 从数组中过滤一些对象
问题描述
我想从数组中过滤一些对象,但目前我只知道删除单个对象,这是我当前的代码看起来像
const deleteChatList = (dispatch) => async (id) => {
dispatch({type: 'delete', payload: id}) // need to change id to list of id or list of object
}
...
const chatListReducer = (state, action) => {
switch (action.type) {
case 'fetch':
return {...state, chatList: action.payload}
case 'delete':
return state.filter((chatList) => chatList.id !== action.payload)
default:
return state;
}
}
任何人都可以帮助我吗?
解决方案
您可以使用Set进行成员资格检查。对象也可能起作用。
const deleteChatList = (dispatch) => async (chatListDeleted) => {
dispatch({type: 'delete', payload: [id1, id2, id3]})
}
...
const chatListReducer = (state, action) => {
switch (action.type) {
case 'fetch':
return {...state, chatList: action.payload}
case 'delete':
const idSet = new Set(action.ids)
return state.filter((chatList) => idSet.has(chartList.id))
default:
return state;
}
}
推荐阅读
- git - Git 在推送时使用了错误的电子邮件 ID
- node.js - 数组包含来自 mongodb 字段的数据无法正常工作以比较值
- java - NetBeans 8.0 + GWT 2.6+
- react-native - 为什么 Android 通知需要 firebase 以及 OneSignal、Pubnub 和 pusher 等第三方通知提供程序?
- css - 如何在联系表格 7 按钮上应用悬停
- node.js - 节点在“等号”处打破 URL 中的 JSON 字符串 =
- node.js - 我可以为 Angular Universal SSR 和客户端渲染提供不同的 baseref 吗?
- json - 在 python3 中解析 Bloomberg API 对 JSON 的响应
- matlab - for循环中的绘图没有可见点
- python - OSError: [WinError 123] 文件名、目录名或卷标语法不正确:'
'(姜戈)