I'm fairly new at creating anonymous functions and using map, what does ...i
mean on the following lines of code. tia
const airlines = this.state.airlines.map(i => (
{ ...i, editing : this.state.editing && i===item }
)
)
I'm fairly new at creating anonymous functions and using map, what does ...i
mean on the following lines of code. tia
const airlines = this.state.airlines.map(i => (
{ ...i, editing : this.state.editing && i===item }
)
)
It's known as spread syntax, and in the current context it involves copying an object. It's the same as doing this:
const airlines = this.state.airlines.map(object => {
object.editing = this.state.editing && object === item;
return object;
});
So what we do is we create a shallow copy of the object, and we add properties to it. This is the purpose of spread syntax - to make a shallow copy, or to collect items for a shallow copy (known as rest syntax - collects the rest of the items). Here's a simplified example of spread syntax:
const arr = [{
name: "Jack"
}, {
name: "Joe"
}];
const res = arr.map(e => ({ ...e,
age: Infinity
}));
console.log(res);
.as-console-wrapper {
max-height: 100% !important;
top: auto;
}