Remove a property from an object without creating a new one (without deleting)

With Lodash omit, for example, to remove object properties using this piece of code:

this.current.obj = omit(this.current.obj, ['sellerSupportWeb', 'sellerSupportAgency', 'sellerSupportAgent'])

But this will create another object this.current.obj, but in my case I need to keep the same object

Do you have an alternative solution? But not an operatordelete

+4
source share
4 answers

You can use lodash _.unset()to remove a property from an object:

var obj = { a: 1, b: 2, c: 3 };

_.unset(obj, 'a');

console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Run code
+2
source

Do you have an alternative solution? But not an operatordelete

. - , , delete ( , , , , ).

, Proxy has, ownKeys, get .. , , . .

+2

. . : , . ( ), delete .

+1

You can set the property undefined, and then when you try to access it, the result will be the same if the property was not.

myObject = {
    myProp = 'myValue'
}

myObject.myProp = undefined;
console.log(myObject.myProp);
console.log(myObject.inexistentProp);
0
source

Source: https://habr.com/ru/post/1684460/


All Articles