I do have a JavaScript code snippet that iterates through an array of fields looking for specific properties and adding those to a dictionary. Also, see this site for another example.
return this.getFields()
.reduce((mappings, field) => ({...mappings, [field.id]: field.name}), {});
This works perfectly fine. But I'm getting an Eslint code style parsing error for the three dots.
Unexpected token ...
Three questions about this.
How can I change my code to avoid the parsing error while keeping the code short?
Shall I disable the ESLint check for this error?
What is the name for the
...
notation?
My workaround would be the following. But I would prefer keeping the original version.
return this.getFields()
.reduce(function(mappings, field) {
mappings[field.id] = field.name;
}, {});
...
notation is for spread operator to return a copy of new array or objects – Axial