Consider the following:
class VillageState {
constructor(place, parcels) {
this.place = place;
this.parcels = parcels;
}
move(destination) {
if (!roadGraph[this.place].includes(destination)) {
return this;
} else {
let parcels = this.parcels.map(p => {
if (p.place != this.place) return p;
return {place: destination, address: p.address};
}).filter(p => p.place != p.address);
return new VillageState(destination, parcels);
}
}
}
The part I'm interested in is here:
let parcels = this.parcels.map(p => {
if (p.place != this.place) return p;
return {place: destination, address: p.address};
}).filter(p => p.place != p.address);
As you can see, the map function called on this.parcels, has two return statements in it. There isn't an else keyword, so I'm wondering how this behaves. Does the initial 'return p' statement return that value to the expression below it? Does it return it to the original function? Or does it allow for two conditions on each item.. as in, if p.place != this.place, return p as is, but for the rest, return an object with these properties/values? Why is the else omitted in this?