Home > Mobile >  Cleanest way to loop over an array of objects and replace a property
Cleanest way to loop over an array of objects and replace a property

Time:09-16

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;
});

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

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; }

  • Related