Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to remove a whole property from a js object [duplicate]

I presume this is an odd question but I have to ask..

If I have a js object that looks like this:

$scope.obj1 = {     "name":"John",     "number":"Eleven",     "color":"Red" } 

Is there a way to remove an entire property? without re-assigning values to a new object.. So if I want to get rid of "number":"Eleven", I would like the object to look like this...

$scope.obj1 = {     "name":"John",     "color":"Red" } 

Thanks!

like image 929
GRowing Avatar asked May 12 '14 22:05

GRowing


People also ask

How do I remove all properties of an object?

Use a for..in loop to clear an object and delete all its properties. The loop will iterate over all the enumerable properties in the object. On each iteration, use the delete operator to delete the current property. Copied!

How will you delete the property of the object in JavaScript?

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 a property from an array of objects?

To remove a property from all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use the delete operator to delete the specific property. The property will get removed from all objects in the array.

How do I remove a property name from an object?

The delete operator is used to remove these keys, more commonly known as object properties, one at a time. The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object.


1 Answers

JavaScript has a delete operator:

delete $scope.obj1.number 
like image 101
zzzzBov Avatar answered Oct 13 '22 23:10

zzzzBov