2

I have two objects and I want to extract only the data that is unique using underscore js.

Object 1 (default)

{
   players: "Players: ",
   tableLimit: "Table Limits:",
   newCardBtn: "Add New Card",
   existingCard: "Use existing one",
   contactUs: "Contact Us",
   test: {
      table: 'test'
   }
}

Object 2 (overwrite)

  {
    players: "Players: ",
    tableLimit: "Table Limits:",
    newCardBtn: "Add New Card",
    existingCard: "Use existing one",
    test: {
      table: 'test'
    }
  }

The end result should return a list with data that is missing from overwrite. In our case, it should return contactUs: "Contact Us"

Till now I have this but it returns all data from default object without the custom:

var def = {
    players: "Players: ",
    tableLimit: "Table Limits:",
    newCardBtn: "Add New Card",
    existingCard: "Use existing one",
    contactUs: "Contact Us",
    test: {
      table: 'test'
   }
}

var custom = {
    players: "Players: ",
    tableLimit: "Table Limits:",
    newCardBtn: "Add New Card",
    existingCard: "Use existing one",
    test: {
      table: 'test'
   }
}

var out = JSON.stringify(Object.assign({}, def, custom));
fs.writeFile("./out.js", out);
Georgi Kirilov
  • 569
  • 2
  • 5
  • 12
  • I know mentioned post is not using `underscore`. If you want answer to be **in underscore only**, please decline it and add necessary information in question. If you are fine with pure JS version, please accept duplicate so that no one else can answer this post – Rajesh Mar 08 '17 at 09:44

1 Answers1

4

This will parse obj1 and if there is no matching property with a matching value in obj2 then it is added to obj3. You can see the results in the output...

var obj1 = {
  players: "Players: ",
  tableLimit: "Table Limits:",
  newCardBtn: "Add New Card",
  existingCard: "Use existing one",
  contactUs: "Contact Us",
};

var obj2 = {
  players: "Players: ",
  tableLimit: "Table Limits:",
  newCardBtn: "Add New Card",
  existingCard: "Use existing one",
};
  
var obj3 = (function() {
  result = {};
  for (var k in obj1) {
    if (obj2[k] != obj1[k]) {
      result[k] = obj1[k];
    }
  }
  return result;
})();

console.log(obj3);
Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
  • Just a question, if you are just going to give a function in pure js that gets difference, its better to mark this as dupe. – Rajesh Mar 08 '17 at 09:41
  • @Rajesh While I agree that the question you proposed as a duplicate will give the OP a working solution, it's also massive overkill for this simple scenario. My answer would never be a working, relevant answer to the question you proposed as a duplicate, so I consider this more suitable than the answer to the question you linked. I think if you don't need a deeper comparison, most people would prefer the look of this code to the linked. – Reinstate Monica Cellio Mar 08 '17 at 10:08
  • 1
    Agree to your point, but I guess this is where things get grey. Your answer is apt for this situation where answer in dupe is more generic. However, for a beginner, your answer will make more sense, but for a experienced reader, this would be like duplicating content and this might attract downvotes. Hope people do consider this difference before choosing their votes. – Rajesh Mar 08 '17 at 10:12