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.
Answer: Use the delete Operator You can use the delete operator to completely remove the properties from the JavaScript object. Deleting is the only way to actually remove a property from an object.
Remove Property from an ObjectThe delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
To remove an element from an array of objects in React: Use the filter() method to iterate over the array. On each iteration check if a certain condition is met. The filter method returns an array containing only the elements that satisfy the condition.
You can't remove a property, unless you remove it permanently, for all cases. What you can do, however, is to create multiple classes, in a class hierarchy, where one class has the property and the other hasn't.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With