【问题标题】:Delete a key from a copy of Map object从 Map 对象的副本中删除一个键
【发布时间】:2019-11-15 10:47:58
【问题描述】:

我正在尝试删除我的 react-native 状态 Map 对象的键。

const [errors, setErrors] = useState<Map<string, string>>(null);
let copy: Map<string,string> = {...errors}
copy["a"] = "a";
setErrors(copy);
let copy2: Map<string,string> = {...errors};
copy2.delete("a"); //Got error here
setErrors(copy2);

但我有这个错误:

copy2.delete 不是函数(在 'copy2.delete("a") 中,'copy2.delete' 是 未定义)

如何处理?

【问题讨论】:

  • 你得到了答案还是没有??
  • 是的,看我自己的回答。
  • 好吧...酷...!

标签: typescript react-native


【解决方案1】:

我的对象的副本是错误的。

const [errors, setErrors] = useState<Map<string, string>>(null);
let copy: Map<string,string> = new Map<string,string>(errors);
copy["a"] = "a";
setErrors(copy);
let copy2: Map<string,string> = new Map<string,string>(errors);
copy2.delete("a");
setErrors(copy2);

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2017-11-20
    • 2014-05-04
    相关资源
    最近更新 更多