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

85
Views
cómo copiar en profundidad un objeto JavaScript de otro sin agregar nuevas propiedades

Aquí tengo un objeto predeterminado con propiedades anidadas, por ejemplo:

 default = { key1: val1; key2: { key3: val3, key4: val4 } }

y un nuevo objeto con nuevos valores para las mismas propiedades que el anterior y algunas propiedades adicionales propias

 newObject = { key1: val11, key2: { key3: val31, key5: val51, } }

Ahora necesito reemplazar todas las propiedades anidadas predeterminadas disponibles con newObject sin agregar ninguna propiedad nueva a la predeterminada. El esqueleto por defecto nunca debe cambiar. También esto necesita ser una copia profunda. Así que el resultado aquí debería verse así:

 result = { key1: val11; key2: { key3: val31, key4: val4, } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Agregué código para usted, pero si necesita más complejo, es posible que desee usar algunos paquetes js, pero creo que debería ser suficiente.

Aquí tienes:

 let objectDefault = { key1: 'val1', key2: { key3: 'val3', key4: 'val4' } } let otherObject = { key1: 'val5', key2: { key3: 'val6', key4: 'val7' }, key10: 'test' } let filteredValuesOfOthedObject = Object.fromEntries(Object.entries(otherObject).filter(([key]) => Object.keys(objectDefault).includes(key))); let newObject = {...objectDefault, ...filteredValuesOfOthedObject} console.log(newObject)
about 4 years ago · Juan Pablo Isaza Report

0

Echa un vistazo a este. // Objeto principal let _default = { key1: 'val1', key2: { key3: 'val3', key4: 'val4', key6: { key7: 'test', key10: 'val10' } }, key11: 'val231 '};

 // Output object let newObj = { key1: 'val11', key2: { key3: 'val31', key5: 'val51', key6: { key7: 'val31', key8: 'val43' }, key12: 'val212' } }; // Utils to check if value if object function isObject(value) { return typeof (value) === 'object' && !(value instanceof Date) && !Array.isArray(value) && !Object.is(value, null) && !Object.is(value, undefined) && !(value instanceof Function) } // Recursive function to add function recursive(obj, ouputObj) { Object.keys(obj).map((el, i) => { let val = obj[el]; if (!ouputObj?.[el]) ouputObj[el] = val; if (isObject(val)) recursive(val, ouputObj?.[el] || {}); }) return ouputObj; } newObj = recursive(_default, newObj); console.log(`~~~~~~~~~~~~~~~, `, newObj);
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!