我有:
interface IMap {
[name: string]: string;
}
var map: IMap = {};
map["S"] = "s";
map["C"] = "c";
map["D"] = "d";
如何完全删除项目地图[" S"]?我不想最终得到一个空对象,所以使用删除地图[" S"]将无法正常工作。
答案 0 :(得分:20)
如何完全删除项目地图[“S”]?我不想以null对象结束,所以使用delete map [“S”]
delete
完全清除:
interface IMap {
[name: string]: string;
}
var map: IMap = {};
map["S"] = "s";
map["C"] = "c";
map["D"] = "d";
delete map["S"];
console.log(map);
console.log(map["S"],map["non-existent"]); // undefined,undefined
console.log(Object.keys(map)); // ["C","D"]