Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an item from an object

Tags:

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.

like image 633
bb2 Avatar asked Jan 07 '15 00:01

bb2


People also ask

How do you remove an element from an object?

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.

How do I remove one 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.

How do you remove an item from an object in react?

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.

How do I remove a property from an object in C#?

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.


1 Answers

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"]  
like image 179
basarat Avatar answered Sep 30 '22 05:09

basarat