0

I need to merge two Javascript objects by its key value mapping.

Here is my first Javascript object

var json1= [
    { ID: 1, limit: 5, name: "foo" }, 
    { ID: 2, limit: 9, name: "dog" }

   ];

Here is my second Javascript object

 var json2 = [
        { ID: 2, validate: false, LastRunTime: "February" }, 
        { ID: 1, validate: true, LastRunTime: "January" }
    ];
 $.extend(true, {}, json1, json2);

this gives me the resultant Javascript like this

[
    {
        {
            ID: 2,
            LastRunTime: "February",
            limit: 5,
            name: "foo",
            validate: false
        },
        {
            ID: 1,
            LastRunTime: "January",
            limit: 9,
            name: "dog",
            validate: true
        }
    }
]

but I am looking for the code that map ID as a key and then merge the Javascript objects like this irrespective of their order in array.

[
    {
        {
            ID: 1,
            LastRunTime: "January",
            limit: 5,
            name: "foo",
            validate: true
        },
        {
            ID: 2,
            LastRunTime: "February",
            limit: 9,
            name: "dog",
            validate: false
        }
    }
]
3
  • What do you want to happen if the merge has a conflict (e.g. each object has a conflicting value for a key)? Commented May 8, 2014 at 6:25
  • 1
    Your resulting JSON is not a possible Javascript object. You can't have { { in an object. Commented May 8, 2014 at 6:26
  • 1
    This has absolutely nothing to do with JSON Commented May 8, 2014 at 6:26

2 Answers 2

2

You need to switch the data representation. The best will be to store your json as:

var json1 = {
    "1": { ID: 1, limit: 5, name: "foo" }, 
    "2": { ID: 2, limit: 9, name: "dog" }
};

If it's impossible then you can convert your data on the fly:

var json1obj = {};
json1.forEach(function(obj) {
  json1obj[obj.ID] = obj;
});
var json2obj = ...;

$.extend(true, {}, json1obj, json2obj);
Sign up to request clarification or add additional context in comments.

Comments

0
var result = [];
for (var i = 0; i < json1.length; i++) {
    var found = false;
    for (var j = 0; j < json2.length; j++) {
        if (json1[i].ID == json2[j].ID) {
            result.push($.extend(true, {}, json1[i], json2[j]));
            found = true;
            break;
        }
    }
    if (!found) {
        // If no match found in json2, put a copy in the result
        result.push($.extend(true, {}, json1[i]));
    }
}

3 Comments

what does break outer means
Sorry, that should just be break. I originally had it breaking the outer loop, and had a label outer: before that. But then I realized I needed to add the code at the bottom that puts a copy of json1[i] in the result if the inner loop doesn't find anything.
thanks barmar and also result.push($.extend(true, {}, json1[i], json2[j]));

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.