I have this nested for-loop that I am having difficulty turning into a one liner. What is the best way to eliminate the nested for? Thanks.
for (const house of this.houses){
for (const room of house.rooms){
this.plans.push(room.layout.id);
}
}
>Solution :
You could map the nested id.
this.plans = this.houses.flatMap(({ rooms }) => rooms.map(({ layout: { id } }) => id));
For better readability:
this.plans = this.houses.flatMap(({ rooms }) => rooms
.map(({ layout: { id } }) => id)
);