javascript - 顽固的 Redux 操作发送空数组
问题描述
我有这段代码:
const result = Object.keys(state).reduce((result, key) => {
if (key.includes('game')) {
result[key] = state[key];
}
return result;
}, []);
this.props.sendBackResult(result);
最后一行当然会触发 Redux 操作。但这不是发送格式良好的数组,而是只发送空数组。这是什么原因?当我插入日志而不是发送操作时,我看到了前一个函数的正确结果。
谢谢!
解决方案
您正在使用一个空数组[]
作为reduce
. 在阵列上设置一个键很可能不是您想要做的事情。
您可以改为推送其中包含的所有键game
对应于数组的值:
const result = Object.keys(state).reduce((result, key) => {
if (key.includes('game')) {
result.push(state[key]);
}
return result;
}, []);
如果您仍然希望最终结果中的键,您很可能希望使用一个对象来代替:
const result = Object.keys(state).reduce((result, key) => {
if (key.includes('game')) {
result[key] = state[key];
}
return result;
}, {});
推荐阅读
- java - InputMismatchException 帮助,如何覆盖?
- linux - 当gdb附加到tracee时tracee会得到SIGSTOP做什么
- pytorch - Google Colab 最近引发错误 ModuleNotFoundError: No module named 'google.cloud.storage.retry'
- python - 从列类型对象 pandas 中删除为空的行
- php - Laravel 控制器存储以驼峰格式接收的数据
- python - 在 Plotly Dash 中结合 DateRangePicker 和 Dropdown
- spring-webflux - Spring Webflux HttpResponse
- java - 为什么 @OnDelete(action = OnDeleteAction.CASCADE) 在删除 InheritanceType.JOINED 时不删除父表条目
- google-cloud-platform - 虚拟机到 Google Cloud Shell 带宽费用
- python - 用于多行替换字符串的正则表达式,然后放入熊猫数据框中