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

92
Views
How can I loop through the keys of an object and set values equal to a destructured variable?

I am trying to simplify and shorten my current code. My code is working fine. This is what I have:

app.patch('/listings/:id', (req, res) => {
    const { id } = req.params;
    console.log(req.body)
    const {
        leaser, propertyType, pricePerDay,
        currency, city, street, description
    } = req.body;
    const foundListing = listings.find(l => l.id === id);
    
    foundListing.leaser = leaser;
    foundListing.pricePerDay = pricePerDay;
    foundListing.propertyType = propertyType;
    foundListing.currency = currency;
    foundListing.city = city;
    foundListing.street = street;
    foundListing.description = description;

    res.redirect('/listings');
})

How can I make the repetitive last half of the code shorter? There is probably a way in which I can write only one line

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I would use the following approach.

app.patch('/listings/:id', (req, res) => {
   const { id } = req.params;
   const foundListing = listings.find(l => l.id === id);

   Object.keys(foundListing).forEach(key => {
     foundListing[key] = req.body[key]
   })

   res.redirect('/listings');
})
about 4 years ago · Juan Pablo Isaza Report

0

You can use Object.assign() in combination with the spread operator to copy the properties of req.body to the foundListing like this:

const foundListing = listings.find(l => l.id === id);
Object.assign(foundListing, ...req.body)

Or even shorter (as you probably don't need the foundListing variable later):

Object.assign(listings.find(l => l.id === id), ...req.body);

Suddenly your code becomes really short:

app.patch('/listings/:id', (req, res) => {
    Object.assign(listings.find(l => l.id === req.params.id), ...req.body);
    res.redirect('/listings');
})

If you want to copy only certain properties of req.body, you could do it like this:

const foundListing = listings.find(l => l.id === id);
["leaser", "pricePerDay", "propertyType", "currency", "city", "street", "description"].forEach((e) => {
  foundListing[e] = req.body[e];
});

about 4 years ago · Juan Pablo Isaza Report

0

If all the foundListing properties are going to be modified, then you could loop through the properties to make changes, like so:

app.patch('/listings/:id', (req, res) => {
    
    const { id } = req.params,
          foundListing = listings.find(l => l.id === id);

    for(const key in foundListing){ 
        foundListing[key] = req.body[key] 
    }

    res.redirect('/listings');
})

If you need to specify which properties are going to be modified then you can list them in an array and loop through them to make changes, like so:

app.patch('/listings/:id', (req, res) => {
    
    const { id } = req.params,
          foundListing = listings.find(l => l.id === id),
          keys = [
             'leaser', 
              'propertyType', 
              'pricePerDay',
              'currency', 
              'city', 
              'street', 
              'description'
          ];

    keys.forEach(key => { foundListing[key] = req.body[key] })
    res.redirect('/listings');
})
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!