http://github.com/swannodette/Set/tree/master
Support for intersection, union, difference on arbitrary Arrays
of JavaScript Objects which are JSON compliant.

You can do things like:

var set1 = new Set([1, 2, 9, [3, 4]]);
var set2 = new Set([1, [3, 4], 5, 9]);

// set1.aintersection(set2) -> [1, [3, 4], 9]

var set3 = new Set([{first:"Bob", last:"Smith"}, {first:"Mary",
last:"Smith"}]);
var set4 = new Set([{first:"Tim", last:"Smith"}, {first:"Mary",
last:"Smith"}]);

// set3.adifference(set4) -> [{first:"Bob", last:"Smith"}, {first:"Tim",
last:"Smith"}]

It includes a function called $hash that can test equality of any two
JavaScript Objects which are JSON compliant. This works on Objects with the
following bit of magic: it converts Objects into arrays of arrays of key
value pairs. It sorts this array by the original hash keys and then
JSON.encodes them. Instead of:

{first:"Bob", last:"Smith"} != {first:"Bob", last:"Smith"}

You can do

$hash({first:"Bob", last:"Smith"}) == $hash({first:"Bob", last:"Smith"})

For large collections which are checked frequently whether they include a
subset of some Objects, it far more efficient than Array.contains, and you
don't have to write additional equality logic since Array.contains cannot
compare  two JavaScript Objects with identical key-value pairs.

Cheers.

Reply via email to