3

I got two JavaScript objects. I want to compare both objects to one another. If object 1 has a key which is missing in object 2 add this key to object 2.

But this must also be the case if object 2 has a key which is missing in object 1.

So for example, object 1 looks like this.

var object_1 = {
    "Address": "Address",
    "AccessDeniedText": "You do not have access to the page you requested.<br>Use the navigation menu on the left to select a page you want to open.",
    "Basket": "Shopping cart",
    "BasketVAT": "VAT {0}"
    ...
};

object 2 looks like this.

var object_2 = {
    "Address": "Address",
    "Basket": "Shopping cart",
    ...
};

In object 2 the key AccessDeniedText and BasketVAT are missing.

Expected result
What i now want is to have an new object, lets say object 3 that would now output the following.

var object_3 = {
    "Address": "Address",
    "AccessDeniedText": "",  // I can fill this missing text
    "Basket": "Shopping cart",
    "BasketVAT": "" // I can fill this missing text
}

I tried to use $.each() and for(key in object). However, this made my whole browser crash. Not so wierd, because each object has more then a 1000 rows.

I also tried to awnser of this question: Javascript: compare two objects, and get key-value pair It didn't work for me.

Is there a simple way to do it? Without my browser crashing of the heavy payload? I dont care using jQuery.

Red
  • 6,599
  • 9
  • 43
  • 85
  • 1
    It looks like you're asking this: https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – Bas van Dijk Jun 07 '17 at 09:22
  • That overwrites properties doens't it? From a voted comment `This doesn't work if objects have same name attributes, and you would also want to merge the attributes.`. Thats not exacly what i want. – Red Jun 07 '17 at 09:26
  • Iterating over 1000 props shouldn't crash browser. It is nothing for modern browsers. – Yury Tarabanko Jun 07 '17 at 09:30
  • 1
    The duplicate is not what i want... It merges 2 objects. I dont want to merge both `key` and `value` only the `key` ... – Red Jun 07 '17 at 09:42

0 Answers0