0

I have two arrays, both of they consists of random keys and values. I have an updated object and current object. But the updated object contains new values and old values. I want to find the differences between those two objects keys and values, to produce the new updated object with new keys or values.

Current Object

{
  key1: "abc"
  key2: "ggg"
  key3: 0,
  key4: ["1","3","5"]
  key5: [1,2,3,4,5]
  key6: [9,8,7,6],
  key7: false
}

Updated Object

{
  key1: "abc"
  key2: "new"
  key3: 30,
  key4: ["1","3","5"]
  key5: [2,3,4]
  key6: [],
  key7: true,
  special8: [1,2,3]
}

Result

{
  key2: "new"
  key3: 30,
  key5: [2,3,4]
  key6: [],
  key7: true,
  special8: [1,2,3]
}
Abel
  • 1,494
  • 3
  • 21
  • 32

3 Answers3

2

Given your conditions: the following reduce() function compares each of your updated object's properties against each of your current object's properties and returns a brand new object containing the differences.

Please note: simply shallowly comparing arrays of strings (such as your key4 properties) using !== can return false negatives, hence the use of JSON.stringify().

// Current Object.
const current = {
  key1: "abc",
  key2: "ggg",
  key3: 0,
  key4: ["1","3","5"],
  key5: [1,2,3,4,5],
  key6: [9,8,7,6],
  key7: false
}

// Updated Object.
const updated = {
  key1: "abc",
  key2: "new",
  key3: 30,
  key4: ["1","3","5"],
  key5: [2,3,4],
  key6: [],
  key7: true,
  special8: [1,2,3]
}

// Difference Object.
const difference = (Object.keys(updated)).reduce((difference, key) => {

  // Same?
  if (JSON.stringify(updated[key]) == JSON.stringify(current[key])) return difference 

  // Different.
  return {...difference, [key]: updated[key]}

}, {})

console.log(difference)
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
0

The following shall solve your need:

"use strict";

let _ = require ('lodash');    

let x = {
  key1: "abc",
  key2: "ggg",
  key3: 0,
  key4: ["1","3","5"],
  key5: [1,2,3,4,5],
  key6: [9,8,7,6],
  key7: false
};

let y = {
  key1: "abc",
  key2: "new",
  key3: 30,
  key4: ["1","3","5"],
  key5: [2,3,4],
  key6: [],
  key7: true,
  special8: [1,2,3]
};

let result = {};

_.each(y,function(value,key){

    //when the key is not present in x at all
    if(!x[key]){
        result[key] = y[key];
    }
    //when the value is of type array
    else if(Array.isArray(y[key])){
        if(y[key].toString() !== x[key].toString()){
            result[key] = y[key];
        }
    }
    //for all other type of values
    else if(y[key] !== x[key]) {
        result[key] = y[key];
    }
});

console.log(result);
Ayan
  • 8,192
  • 4
  • 46
  • 51
0

Here is just another solution:

We are going to use two different functions. An equals functions for the purpose of seeing if equal specific objects are equals:

function equals(obj1, obj2) {
    let equals = true;
    if (!obj1) return;
    if (obj1.length == obj2.length) {
        for (var index in obj1) {
            if (obj1[index] != obj2[index]) {
                return false;
            }
        }
    } else return false;
    return true;
}

Then, the actual printDifferences function which utilizes the above function to check the differences of two objects. This is then returned in the form of another object.

function printDifferences(object1, object2) {
    let newObj = {};
    for (var key in object2) {
        if ((typeof object1[key] == "object" && !equals(object1[key], object2[key])) ||
            (typeof object1[key] != "object" && object1[key] != object2[key]) ||
            object1[key] == undefined) {
            newObj[key] = object2[key];
        }
    }
    return newObj;
}

console.log(printDifferences(current, updated))
Shawn31313
  • 5,978
  • 4
  • 38
  • 80