Skip to content

Instantly share code, notes, and snippets.

@astroanu
Last active February 22, 2017 21:25
Show Gist options
  • Save astroanu/cf76c97de0f8d2e490f5 to your computer and use it in GitHub Desktop.
Save astroanu/cf76c97de0f8d2e490f5 to your computer and use it in GitHub Desktop.
Underscore js object difference
var Objcontains = function(oldO, newO) {
var changed = {};
_.each(newO, function(v, i) {
if (oldO[i] != undefined) {
if (Array.isArray(v)) {
changed[i] = _.difference(v, oldO[i]);
} else if (typeof v == 'object') {
changed[i] = Objcontains(oldO[i], v);
} else {
if (oldO != undefined && (oldO[i] == undefined || oldO[i] != v)) {
changed[i] = v;
}
}
} else {
changed[i] = v;
}
});
return changed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment