javascript - 如何将状态值分配给另一个对象?
问题描述
我已经尝试了下面的所有内容。
它不工作有什么原因吗?
有什么建议吗?
感谢您的友好回复。
addInfo = () => {
const hash = this.state.hash;
//test if all required fields are provided
if (this.state.firstName && this.state.lastName) {
allMap[hash] = Object.assign(this.state);
/*
tried these too
allMap[hash] = Object.assign({}, this.state);
allMap[hash] = {...this.state};
allMap[hash] = this.state
*/
this.reset();
} else {
alert("please fill the required fields")
}
};
解决方案
那是因为你没有将它分配给任何东西,你的第一个参数应该是空的{}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
allMap[hash] = Object.assign({}, this.state);
或者你可以这样做:
allMap[hash] = {...this.state}
推荐阅读
- android - AWS AppSync onUpdate 订阅在模拟器上不起作用
- c++ - 错误:“struct cv::aruco::DetectorParameters”没有名为“cornerRefinementMethod”的成员
- javascript - 使用多参数函数时无法获取对象数据
- typescript - 如何忽略打字稿中的故事书插件错误?
- grep - Grep 识别目录中包含全零的文件并列出或删除它们
- android - ImageView 中的标记,如 instagram 有
- c++ - 在我的 3D perlin 噪声中,Z 轴上出现伪影的原因是什么?
- javascript - Discord.js:无法读取 Null 的属性“名称” - 如何解决此问题?
- python - 使用内部连接连接两个数据框
- ms-access - 我可以重新分发 dao360.dll 和 acedao.dll 吗?