首页 > 解决方案 > 打字稿更新对象内的项目

问题描述

假设我有这个对象:

info: {
   firstName: Joe,
   lastName: Smith,
   userName: jsmith
}

然后通过一个函数,我想只更新 info 中的 userName ,因为他userName是 now jsmith1。我将如何去做这样的事情?

标签: javascripttypescript

解决方案


info.userName = "jsmith1";

或者...

info["userName"] = "jsmith1";

如果您有兴趣了解 JavaScript 对象,请阅读本文

这个答案还解释了何时使用括号表示法与点表示法


推荐阅读