1

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);
4
  • 3
    Interesting names choice objectOne and objectTwo are actually Arrays ;) Commented Aug 6, 2021 at 18:31
  • What is the expected result? Commented Aug 6, 2021 at 18:32
  • I thought I explained that in the description, but the end result is to have one a single array carrying objects with less null values. Commented Aug 6, 2021 at 18:34
  • What to do when neither object has a null value for a given key? Commented Aug 6, 2021 at 18:37

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Very nice, I forgot about the reduce method. This makes it much cleaner.
1

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);

Comments

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' }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.