繁体   English   中英

删除打字稿中的键值对的正确方法是什么

[英]What is the proper way to remove a key-value pair in typescript

我进行了以下实验,发现“删除”可用于删除键值对。 我的问题是:这是“正确”的做法吗?

let myMap:{[key:string]:string} = {};

myMap["hello"] = "world";
console.log("hello="+myMap["hello"]); // it prints 'hello=world'

delete myMap["hello"];
console.log("hello="+myMap["hello"]); // it prints 'hello=undefined'

我的问题是:这是“正确”的做法吗?

这是准确的方法,但是有两个警告

  • 除非该属性不可配置
  • 属性是继承的

例如,您不能删除location href属性

delete location.href //returns false since this property cannot be deleted

演示

 var b = { a: 1, b: 2 }; Object.defineProperty(b, "c", { enumerable: true, configurable: false, writable: true, value: 3 }); delete bc; console.log(b); //all properties intact 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM