Last active
February 22, 2017 21:25
-
-
Save astroanu/cf76c97de0f8d2e490f5 to your computer and use it in GitHub Desktop.
Underscore js object difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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