intersect(second[, comparer, arg])

suckgamoni edited this page May 15, 2013 · 1 revision

Produces the set intersection of two sequences by using the specified equality comparer to compare values.

Syntax

Parameters

second

An iteratable object whose distinct elements that also appear in the first sequence will be returned.

comparer

Type: function(value1, value2, arg): boolean
An equality comparer to compare values.

arg

An external argument.

Return Value

A sequence that contains the elements that form the set intersection of two sequences.


Example

var id1 = [ 44, 26, 92, 30, 71, 38 ];
var id2 = [ 39, 59, 83, 47, 26, 4, 30 ];

var both = from(id1).intersect(id2);

both.each("console.log($)");

/*
 This code produces the following output:

 26
 30
*/
var store1 = [ { name: "apple", code: 9 }, 
               { name: "orange", code: 4 } ];

var store2 = [ { name: "apple", code: 9 }, 
               { name: "lemon", code: 12 } ];

// Get the products from the first array 
// that have duplicates in the second array.

var comparer = "#0 == #1 || (#0 && #1 && #0.name == #1.name && #0.code == #1.code)";

var duplicates = 
    from(store1).intersect(store2, comparer);

duplicates.each("console.log($name + ' ' + $code)");

/*
    This code produces the following output:
    apple 9
*/