Skip to content

Instantly share code, notes, and snippets.

@ainthek
Created September 24, 2015 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ainthek/8928c23b7298296824a5 to your computer and use it in GitHub Desktop.
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)
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