[英]What is the proper way to remove a key-value pair in typescript
I conducted the following experiment, and discovered that 'delete' can be used to remove a key-value pair. 我进行了以下实验,发现“删除”可用于删除键值对。 My question is: is this the 'proper' way to do it? 我的问题是:这是“正确”的做法吗?
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'
My question is: is this the 'proper' way to do it? 我的问题是:这是“正确”的做法吗?
This is the accurate way of doing it, but there are two caveats 这是准确的方法,但是有两个警告
For example you cannot delete href
property of location
例如,您不能删除location
href
属性
delete location.href //returns false since this property cannot be deleted
Demo 演示
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.