[英]What is the best way to add key-value pair(s) to a JS object present in an array
[英]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.