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

161
Views
¿Cuál es la forma eficiente de clonar superficialmente un objeto usando JavaScript?
//function for creating a shallow object and remove the multiple spaces export const getTrimmedValues = (values) => { const shallowValues = { ...values, }; for (const key in shallowValues.primaryInformation) { const currentValue = shallowValues.primaryInformation[key]; if (typeof currentValue === 'string') { shallowValues.primaryInformation[key] = currentValue.replace(/\s+/g, ' ').trim(); } } return shallowValues; }; //Original Object const values = { otherObject: {} otherArray: [] primaryInformation: { email: "test.test@testdata.com" externalId: "DSB-External test" firstName: "Dave External test test" isGood: true isHeaven: false lastName: "Bacay External" userId: 656555 } } //calling the function getTrimmedValues(values)

Quiero crear un objeto poco profundo a partir del objeto original y editar la cadena para eliminar los espacios múltiples usando un objeto poco profundo y un bucle for , creo que lo implementé de manera incorrecta.

Todos los consejos y comentarios son apreciados.

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

0

Aquí podemos aprovechar que la función JSON.stringify tiene un segundo parámetro, ya que una función de reemplazo itera internamente en cada clave del objeto. Por favor, compruebe el siguiente código.

 //Original Object const values = { otherObject: {}, otherArray: [], primaryInformation: { email: "dave.external@testdata.com", externalEmployeeId: "DSB-External test ", firstName: "Dave External test test", isActive: true, isExternal: false, lastName: "Bacay External", userId: 656555, } }; function getTrimmedValues(obj) { let str = JSON.stringify(obj, (key, value) => { if(typeof value === 'string') { return value.replace(/\s+/g, ' ').trim() } return value; }); return JSON.parse(str); } console.log(getTrimmedValues(values));

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!