Created
September 24, 2015 13:51
-
-
Save ainthek/8928c23b7298296824a5 to your computer and use it in GitHub Desktop.
compare two arrays and produceintersection, a diff b b diff a (compare is based on lengths/indexed, not sparse arrays)
This file contains hidden or 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 oldA = ['o1', 'o2', 'o3', 'o4'], | |
newA = ['n0', 'n1', 'n2']; | |
//var oldA=['o1','o2'],newA=['n0','n1','n2']; | |
//var oldA=['o1','o2'],newA=['n0','n1']; | |
var changed = oldA.slice(0, Math.min(oldA.length, newA.length)); | |
var added = newA.slice(oldA.length); | |
var deleted = oldA.slice(newA.length); | |
console.log(changed, added, deleted); | |
function array_indexDiff(a, b) { | |
return { | |
"a ∩ b": a.slice(0, Math.min(a.length, b.length)), | |
// b not in a | |
"b \ a": b.slice(a.length), | |
// a not in b | |
"a \ b": a.slice(b.length) | |
} | |
} | |
var d = array_indexDiff(oldA, newA); | |
console.log(d["a ∩ b"], d["b \ a"], d["a \ b"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment