Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Cleanest way to loop over an array of objects and replace a property

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;
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!