0

I am trying to merge two json array with objects as element. You may refer to this plunkr file for both json. I have succesfully retrieve the expected final outcome array id, but I do not know how to form back the expected json as below. I am using underscore js for this purpose.

Note: If object exist in newJson and not in currentJson, after merge, it will be inactive state by default.

I am not sure whether I am using the correct approach. This is what I have try:

var newJsonID = _.pluck(newJson, 'id');
var currentJsonID =  _.pluck(currentJson, 'id');
var union = _.union(newJsonID, currentJsonID);
var intersection = _.intersection(currentJsonID, newJsonID);
var final = _.difference(union, _.difference( currentJsonID, intersection);

Expected Final Outcome:

   [
    {
        "id": "12",
        "property1Name": "1"
        "status": "inactive"
    },
    {
        "id": "11",
        "property1Name": "1"
        "status": "inactive"
    },
    {
        "id": "10",
        "property1Name": "1"
        "status": "inactive"
    },
    {
        "id": "9",
        "property1Name": "1"
        "status": "active"
    }
]
3
  • where do you get the status from? i mean with the same id? Commented May 18, 2016 at 15:53
  • @NinaScholz do you mean where do I get the status from expected final outcome? If the id in newJson do exist in currentJson, take the currentJson status of same id, else default to inactive. Commented May 18, 2016 at 16:22
  • are you only interested in a underscore solution? Commented May 18, 2016 at 16:25

1 Answer 1

2

A solution in plain Javascript with two loops and a hash table for lookup.

function update(newArray, currentArray) {
    var hash = Object.create(null);
    currentArray.forEach(function (a) {
        hash[a.id] = a.status;
    });
    newArray.forEach(function (a) {
        a.status = hash[a.id] || 'inactive';
    });
}

var newJson = [{ "id": "12", "property1Name": "1" }, { "id": "11", "property1Name": "1" }, { "id": "10", "property1Name": "1" }, { "id": "9", "property1Name": "1" }],
    currentJson = [{ "id": "10", "property1Name": "1", "status": "inactive" }, { "id": "9", "property1Name": "1", "status": "active" }, { "id": "8", "property1Name": "1", "status": "active" }, { "id": "7", "property1Name": "1", "status": "inactive" }];
   
update(newJson, currentJson);
document.write('<pre>' + JSON.stringify(newJson, 0, 4) + '</pre>');

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

3 Comments

Oops. I have missed out the property1Name in the final expected solution. Basically, new Json could have a lot of property name, eg: property1Name, property2Name etc, while currentJson will have extra 1 property which is status. Based on this solution you give, I have to hard code return { id: a.id, property1Name: a.property1Name, status: hash[a.id] }, which does not seems to be optimal for me as when an extra property is being added in the json (retreive from db), I have to change the code in javascript again or else it will not be reflected. Is there any possible solution to avoid this?
would you like to add only the status property to newJson?
basically the final outcome should have all the existing property that is available in newJson plus status.

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.