0

Say I have an object that looks like this

const props = {
    delete: function() {
        // stuffs
    },
    hello: 'hello',
    other: 'other', 
}

Now say I use spread operators and do something like this

const {hello, ...otherStuffs} = props;

Then for otherStuffs, I still get an object that is a copy of props but except for the hello key.

But what if I do not want the delete key of the object ? I can not do the same as done above because apparently delete is a reserved keyword.

const {delete, ...otherStuffs} = props; // error here

I can however still filter the keys from the object that are not equal to 'delete' and get my object but is there a way to do this using spread operators ?

Prasanna
  • 4,125
  • 18
  • 41

1 Answers1

3

You can do it by aliasing the delete property of props. You can try this

const props = {
    delete: function() {
        console.log("delete");
    },
    hello: 'hello',
    other: 'other', 
}

const {"delete": del, ...otherStuffs} = props;
del();

Reference: Invalid JavaScript identifier as a property name

maazadeeb
  • 5,922
  • 2
  • 27
  • 40