Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
spread and destructuring object

I have this code :

function logInfos(user = {}) {
  const redactedUser = {
    firstName: "<REDACTED>",
    lastName: "<REDACTED>",
    address: {
      city: "<REDACTED>",
      country: "<REDACTED>",
    },
  };

  const {
    firstName,
    lastName,
    address: { city, country },
  } = user;

  console.log("partie user", firstName);
  console.log("partie user", lastName);

  const newUser = {
    ...user,
    address: {
      ...user.address,
    },
  };
  console.log("partie newuser", newUser);

  console.log(`${newUser.firstName} ${newUser.lastName} lives in ${newUser.address.city}, ${newUser.address.country}.`);
}

How can I replace the value undefined of user object passed as argument and use the default value of redactedUser object instead?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function compareObj(defaultObj, targetObj) {
for(let key in defaultObj) {
  if(!Array.isArray(defaultObj[key]) && defaultObj[key] !== null && typeof defaultObj[key] === "object") {
    targetObj[key] = {};
    copyObj(defaultObj[key], targetObj[key]); 
  } else {
    if(!targetObj[key]) targetObj[key] = defaultObj[key];
  }
}

}

compareObj(redactedUser, user);

You can place this code function inside logInfos. What it does is, it iterates through all the properties of the default object and checks if the property exists in the target object. In case the property does not exist in the target object, same property will be created in the target object and the value will be copied.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda