In my Angular application, I am attempting to copy an object and add a new property using the spread operator. To add the new property, I have created a method called 'addNewProperty(name)' which returns the property and its value. However, when I try to call this method using the spread operator, I encounter an error mentioning Unexpected token.
Here is the code snippet:
this.files = files['results'].map(file => ({...file, this.addNewProperty(file.name)}));
addNewProperty(name) {
return {
extension: name.split('-')[1]
};
}
Interestingly, when I use the "traditional" Object.assign() method instead, everything works smoothly:
this.files = files['results'].map(file => Object.assign(file, this.addNewProperty(file.name)));
I would appreciate it if someone could explain why Object.assign() functions correctly for copying objects, while trying to achieve the same with {..., myMethod()} does not work as expected.