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!
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!
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.
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.
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.
JavaScript has a delete
operator:
delete $scope.obj1.number
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