0

I need to merge two JSON objects.

First object:

var objectA = {
  "UUID1": {
    "user": {
      "ID": "1"
    }
  },
  "UUID2": {
    "user": {
      "ID": "2"
    }
  },
  "UUID3": {
    "user": {
      "ID": "3"
    }
  }
}

Second object:

var objectB = {
  "UUID4": {
    "user": {
      "ID": "4"
    }
  },
  "UUID5": {
    "user": {
      "ID": "3"
    }
  },
  "UUID6": {
    "user": {
      "ID": "2"
    }
  }
}

Expected result:

{
  "UUID1": {
    "user": {
      "ID": "1"
    }
  },
  "UUID2": {
    "user": {
      "ID": "2"
    }
  },
  "UUID3": {
    "user": {
      "ID": "3"
    }
  },
  "UUID4": {
    "user": {
      "ID": "4"
    }
  }
}

The trick is, the UUID will differ, but the primary key is the user ID. So, I need to compare the user IDs and keep only one UUID.

Is there a clever way on how to approach this? Nested loops using Object.keys(objectX).forEach did not work for me well :(

Thank you!

2
  • Did you try stackoverflow.com/q/9134649/3365113 Commented Sep 13, 2017 at 15:52
  • Yes, but I can't use jQuery I'm afraid. Commented Sep 13, 2017 at 15:53

2 Answers 2

1

You can just create custom function to handle this for you. Something like this:

var objectA = {
  "UUID1": {"user": {"ID": "1"}},
  "UUID2": {"user": {"ID": "2"}},
  "UUID3": {"user": {"ID": "3"}}
}

var objectB = {
  "UUID4": {"user": {"ID": "4"}},
  "UUID5": {"user": {"ID": "3"}},
  "UUID6": {"user": {"ID": "2"}}
}

function merge() {
  var result = {};
  var ids = [];
  for (var i = 0; i < arguments.length; i++) {
    for (var uuid in arguments[i]) {
      if (~ids.indexOf(arguments[i][uuid].user.ID)) {
        continue;
      }
      result[uuid] = arguments[i][uuid];
      ids.push(arguments[i][uuid].user.ID);
    }
  }
  return result;
}

var merged = merge(objectA, objectB);
console.log(merged);

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

1 Comment

Thanks a lot, I've finally used this version to solve my problem!
1

You could take a hash table for remembering the inserted ID and build a new object out of the given objects.

var objectA = { UUID1: { user: { ID: "1" } }, UUID2: { user: { ID: "2" } }, UUID3: { user: { ID: "3" } } },
    objectB = { UUID4: { user: { ID: "4" } }, UUID5: { user: { ID: "3" } }, UUID6: { user: { ID: "2" } } },
    hash = Object.create(null),
    result = [objectA, objectB].reduce(function (r, o) {
        Object.keys(o).forEach(function (k) {
            if (!hash[o[k].user.ID]) {
                hash[o[k].user.ID] = true;
                r[k] = o[k];
            }
        });
        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Great answer, too! Sadly, I can only accept one as "answered" and @codtex was just a bit faster with his. Thanks a lot!

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.