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

238
Views
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 answers
Answer question

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 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!