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

239
Vistas
what am i doing wrong? Code not going through. not returning second parameter

return single object. return second parameter with first parameter values. return parameter not hard coded.

function addPropertiesToObject(obj1, obj2) {
var param = {}
for (var things in obj1) {
    param[things] = obj1[things];
}
for (var things in obj2) {
    param[things] = obj2[things];
}
return param;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Again, if both objects have the same key, that value will be overwritten by the second.

Edit to show adding obj1 to obj2 instead of obj2 to obj1

function addPropertiesToObject(obj1, obj2) {
  var param = {}
  for (var things in obj2) {
    param[things] = obj2[things];
  }
  for (var things in obj1) {
    param[things] = obj1[things];
  }
return param;
}

const obj1 = {name: "John"}
const obj2 = {name: "Jane"}
const obj3 = {email: "example@domain.com"}
console.log(addPropertiesToObject(obj1,obj2))
//Return: {name: "John"}
console.log(addPropertiesToObject(obj1,obj3))
//Return: {email: "example@domain.com", name: "John"}

You can also accomplish this without looping

function addPropertiesToObject(obj1, obj2) {
return {...obj2,...obj1}
}

const obj1 = {name: "John"}
const obj2 = {name: "Jane"}
const obj3 = {email: "example@domain.com"}
console.log(addPropertiesToObject(obj1,obj2))
//Return: {name: "Jane"}
console.log(addPropertiesToObject(obj1,obj3))
//Return: {email: "example@domain.com", name: "John"}

If you would like to keep the contents of both objects, you can do something like:

function addPropertiesToObject(obj1, obj2) {
  var param = {...obj2}
  for (var things in obj1) {
    if(param[things]) param[things] = [param[things], obj1[things]]
    else param[things] = obj1[things];
  }
return param;
}

const obj1 = {name: "John"}
const obj2 = {name: "Jane"}
const obj3 = {email: "example@domain.com"}
console.log(addPropertiesToObject(obj1,obj2))
//Return: {name: ["Jane","John"]}
console.log(addPropertiesToObject(obj1,obj3))
//Return: {email: "example@domain.com", name: "John"}

Or changing the name of the key like:

function addPropertiesToObject(obj1, obj2) {
  var param = {...obj2}
  for (var things in obj1) {
    if(param[things]) param[things+"1"] = obj1[things]
    else param[things] = obj1[things];
  }
return param;
}

const obj1 = {name: "John"}
const obj2 = {name: "Jane"}
const obj3 = {email: "example@domain.com"}
console.log(addPropertiesToObject(obj1,obj2))
//Return: {name: "Jane", name1: "John"}
console.log(addPropertiesToObject(obj1,obj3))
//Return: {email: "example@domain.com", name: "John"}

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