0

What is the cleanest way to assign a person's seat while looping over the available seats, when after assigning a person's seat, the movie ticket status must be marked unavailable. Would be open to using lodash methods as well.

const movieTickets = [ { seat: "16B" status: "available" }, { seat: "16c" status: "available" }, { seat: "16D" status: "available" } ] const people = [ { name: "Bob" seat: "" }, { name: "Susan" seat: "" }, { name: "Timmy" seat: "" } ] /** * Current solution * The problem is that ticket is not marked as unavailable, and I'm unsure of the cleanest way to do that, would love suggestions here **/ const assignedPeople = people.map(person => { person.seat = movieTickets.find(ticket => ticket.status === "available").seat return person; }); 

3 Answers 3

2

Update your movieTickets array with the same process.

const movieTickets = [ { seat: "16B", status: "available" }, { seat: "16c", status: "available" }, { seat: "16D", status: "available" } ]; const people = [ { name: "Bob", seat: "" }, { name: "Susan", seat: "" }, { name: "Timmy", seat: "" } ] const assignedPeople = people.map(person => { const node = movieTickets.find(ticket => ticket.status === "available"); person.seat = node.seat; node.status = "unavailable"; return person; }); console.log(assignedPeople); console.log(movieTickets);

If you dont want to create a new array, you could update the original array itself.

const movieTickets = [ { seat: "16B", status: "available" }, { seat: "16c", status: "available" }, { seat: "16D", status: "available" } ]; const people = [ { name: "Bob", seat: "" }, { name: "Susan", seat: "" }, { name: "Timmy", seat: "" } ] people.forEach(person => { const node = movieTickets.find(ticket => ticket.status === "available"); person.seat = node.seat; node.status = "unavailable"; }); console.log(people); console.log(movieTickets);

Sign up to request clarification or add additional context in comments.

1 Comment

Dope, didn't see this in front of me, thanks!
0

Assign the found ticket to a variable. Then you can mark it unavailable before assigning the seat to the person.

Also, this allows you to check that a ticket was found. Your code will get an error if there are more people than available tickets, because it will try to access the seat property of null.

const movieTickets = [ { seat: "16B", status: "available" }, { seat: "16c", status: "available" }, { seat: "16D", status: "available" } ]; const people = [ { name: "Bob", seat: "" }, { name: "Susan", seat: "" }, { name: "Timmy", seat: "" } ]; const assignedPeople = people.map(person => { let ticket = movieTickets.find(ticket => ticket.status === "available"); if (ticket) { ticket.status = "unavailable"; person.seat = ticket.seat; } return person; }); console.log(assignedPeople);

Comments

0

You can easily achieve the result using map and find

const movieTickets = [ { seat: "16B", status: "available" }, { seat: "16c", status: "available" }, { seat: "16D", status: "available" } ]; const people = [ { name: "Bob", seat: "" }, { name: "Susan", seat: "" }, { name: "Timmy", seat: "" } ]; const newPeopleData = people.map((p) => { const isSeatAvailable = movieTickets.find((o) => o.status === "available"); if (isSeatAvailable) { isSeatAvailable.status = "unavailable"; return { ...p, seat: isSeatAvailable.seat }; } return p; }); console.log(newPeopleData); console.log(movieTickets);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.