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

107
Views
I have two objects and i need to merge them and concat strings if two props have the same key

I have two objects and i need to merge them and concat strings if two props have the same key note: I'm using lodash.js

obj1 = {
  name: 'john', 
  address: 'cairo'
}

obj2 = {
  num : '1', 
  address: 'egypt'
}

I need the merged object to be like:

merged = {name:"john", num: "1", address:"cairo,Egypt"}

the issue with address prop when I'm trying to use _.defaults(obj1,obj2) or _.merge(obj1,obj2)

the address value will be the value of the address prop inside obj2 so I need a way to merge the two objects and concat the two address props in each object.

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

0

Try this:

    function merge(obj1, obj2) {
      let merged = {
        ...obj1,
        ...obj2
      };
      Object.keys(obj1).filter(k => obj2.hasOwnProperty(k)).forEach((k) => {
        merged[k] = obj1[k] + "," + obj2[k]
      })
      return merged
    }

    obj1 = {
      name: 'john',
      address: 'cairo'
    }

    obj2 = {
      num: '1',
      address: 'egypt'
    }

    console.log(merge(obj1, obj2))

about 4 years ago · Juan Pablo Isaza Report

0

function mergeObjects(objArr) {
    const newObj = {};

    objArr.forEach((element) => {
        const keys = Object.keys(element);
        keys.forEach((key) => {
            if (newObj[key]) newObj[key] = newObj[key] + "," + element[key];
            else newObj[key] = element[key];
        });
    });
    return newObj;
}

const obj1 = {name: 'john',   address: 'cairo'}

const obj2 = {  num : '1',   address: 'egypt'}
console.log(mergeObjects([obj1, obj2]));

This way you can merge any number of objects!

about 4 years ago · Juan Pablo Isaza Report

0

You don't even need lodash. You can easily create you own function for that. You can do it like this:

  • new Set - to find all unique keys across both input objects
  • for...of - to iterate over unique keys calculated above

function mergeObjects(obj1, obj2) {
  let merged = {};
  for (const property of [...new Set([...Object.keys(obj1),...Object.keys(obj2)])]) {
    if(obj1[property]) merged[property] = obj1[property];
    if(obj2[property]) merged[property] = merged[property] ? `${merged[property]},${obj2[property]}` : obj2[property];
  }
  return merged;
}

const obj1 = { name: 'john', address: 'cairo' };
const obj2 = { num : '1', address: 'egypt' };

const mergedObj = mergeObjects(obj1, obj2);
console.log(mergedObj);

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!