bb2
bb2

Reputation: 2982

How to remove an item from an object

I have:

var map = {};
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.

Upvotes: 25

Views: 26585

Answers (1)

basarat
basarat

Reputation: 276239

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

delete does clear it completely:

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

Upvotes: 43

Related Questions