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

153
Views
How to merge two objects containing same keys but different values

I'm wanting to merge two array that carry objects with the same keys, but one object may have a value, but the other object, carrying the same key, may have a null value. I'm wanting to merge the two to try and replace all the null values and have one final object.

I've written the following, but at the moment, I'm only getting undefined as the final result.

const objectOne = [
  {
    one: null,
    two: "Two",
    three: null
  }
];

const objectTwo = [
  {
    one: "One",
    two: null,
    three: "Three"
  }
];

const compareObjects = (searchKey, searchValue) =>
  objectTwo.map((retrieve) =>
    Object.entries(retrieve).forEach(([retrieveKey, retrieveValue]) => {
      if (!searchValue) {
        objectOne.searchKey = retrieveValue;
        console.log(objectOne);
      }
    })
  );

const newObject = objectOne.map((search) =>
  Object.entries(search).forEach(([searchKey, searchValue]) =>
    compareObjects(searchKey, searchValue)
  )
);

console.log(newObject);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Reduce seems more useful in this case than forEach. Reduce will build a new thing from the input and return that.

const objectOne = [
  {
    one: null,
    two: "Two",
    three: null
  }
];

const objectTwo = [
  {
    one: "One",
    two: null,
    three: "Three"
  }
];

const merged = objectOne.map((item, idx) => {
  return Object.entries(item).reduce((acc, [key, value]) => {
    acc[key] = value === null && objectTwo.length > idx ? objectTwo[idx][key] : value;
    return acc;
  }, {})
});
console.log(merged);

about 4 years ago · Juan Pablo Isaza Report

0

One method you can use is to create a function that combines/merges the two dictionaries. First, we create our newObject:

const newObject = [

]

Then, we create our function using an O(n^2) approach and call it:

combine(objectOne,objectTwo)
console.log(newObject)

function combine(objectOne, objectTwo){
    for (let [key1,value1] of Object.entries(objectOne[0])){
        for (let [key2,value2] of Object.entries(objectTwo[0])){
            if(key1 == key2){
                if(value1 == null){
                    newObject.push({
                        key: key1,
                        value: value2
                    })
                }
                else{
                    newObject.push({
                        key: key1,
                        value: value1
                    })
                }
            }
        }
    }
}

This is the following output:

[
  { key: 'one', value: 'One' },
  { key: 'two', value: 'Two' },
  { key: 'three', value: 'Three' }
]
about 4 years ago · Juan Pablo Isaza Report

0

Your mapping of keys/entries is pretty close. It might be easiest to do something like this though:

const objectOnes = [
  {
    one: null,
    two: "Two",
    three: null
  },
  {
     a: 123,
     b: undefined,
  },
];

const objectTwos = [
  {
    one: "One",
    two: null,
    three: "Three"
  },
  {
      b: 456,
  }
];

function merge(a, b) {
    const result = { ...a }; // copy a
    for (const key in result) {
        // Let's say we want to pick the non-null/undefined value
        if (result[key] !== null && result[key] !== undefined) continue;
        result[key] = b[key]; // copy from b
    }
    return result;
}

const merged = objectOnes.map((obj, i) => merge(obj, objectTwos[i]));
console.log(merged);

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!