typescript 如何从Typescript中的字符串索引数组中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27809577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:57:23  来源:igfitidea点击:

How to remove item from string index array in Typescript

typescript

提问by bb2

I have:

我有:

interface IMap {
[name: string]: string;
}

var map: IMap = {};
map["S"] = "s";
map["C"] = "c";
map["D"] = "d";

How can I fully remove item map["S"]? I don't want to end up with a null object so using delete map["S"] wouldn't work.

如何完全删除项目映射 ["S"]?我不想得到一个空对象,所以使用 delete map["S"] 是行不通的。

回答by basarat

How can I fully remove item map["S"]? I don't want to end up with a null object so using delete map["S"]

如何完全删除项目映射 ["S"]?我不想最终得到一个空对象,所以使用 delete map["S"]

deletedoes clear it completely:

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"]