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

101
Views
¿Cómo puedo recorrer las claves de un objeto y establecer valores iguales a una variable desestructurada?

Estoy tratando de simplificar y acortar mi código actual. Mi código está funcionando bien. Esto es lo que tengo:

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

¿Cómo puedo acortar la última mitad repetitiva del código? Probablemente hay una manera en la que puedo escribir solo una línea

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

0

Yo usaría el siguiente enfoque.

 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

Puede usar Object.assign() en combinación con el operador de propagación para copiar las propiedades de req.body en el foundListing de esta manera:

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

O incluso más corto (ya que probablemente no necesite la variable foundListing más adelante):

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

De repente, su código se vuelve realmente corto:

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

Si desea copiar solo ciertas propiedades de req.body , puede hacerlo así:

 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

Si se van a modificar todas las propiedades de foundListing , puede recorrer las propiedades para realizar cambios, así:

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

Si necesita especificar qué propiedades se van a modificar, puede enumerarlas en una matriz y recorrerlas para realizar cambios, así:

 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!