Find the union, intersection, and difference of two ID lists.
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const old = new Set([101, 102]); const next = new Set([102, 103]); [old.union(next), old.intersection(next), old.difference(next)] .map(ids => [...ids].join(', ')).join('\n'); """).AsString);
Unzip the example, then dotnet run.
Prints the union, intersection, and difference, in order:
101, 102, 103 102 101
The full program
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const before = new Set([101, 102, 102, 104]); const after = new Set([102, 103, 104]); const results = { all: [...before.union(after)], kept: [...before.intersection(after)], removed: [...before.difference(after)], added: [...after.difference(before)] }; if (JSON.stringify(results) !== '{"all":[101,102,104,103],"kept":[102,104],"removed":[101],"added":[103]}') { throw new Error('Unexpected ID sets'); } Object.entries(results) .map(([label, ids]) => `${label}: ${ids.join(', ')}`) .join('\n'); """).AsString);
dotnet run prints:
all: 101, 102, 104, 103 kept: 102, 104 removed: 101 added: 103
The download checks the results against the expected IDs before printing. The repeated 102 in the first input appears only once. Pass a Set or another set-like object as the argument: an array by itself lacks the required methods, so passing it directly throws a JavaScript TypeError.
Variations
Find all changes at once. Use symmetricDifference when you want IDs that appear in exactly one set without separating additions from removals:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const before = new Set([101, 102, 104]); const after = new Set([102, 103, 104]); [...before.symmetricDifference(after)].join(', '); """).AsString);
101, 103Match IDs against a map. A Map is set-like: intersection uses its keys, not its values:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const selected = new Set([101, 102]); const labels = new Map([[102, 'Ready']]); [...selected.intersection(labels)].join(', '); """).AsString);
102Related recipes
The short answer
Run JavaScript’s Set methods inside OneEcma. For sets named before and after, before.union(after) gives every distinct ID, before.intersection(after) gives IDs present in both, and before.difference(after) gives IDs no longer present. The C# example prints the results as text. The original sets remain available unchanged.
Use OneEcma when the set logic is already written in JavaScript and shared with another app; if the logic lives entirely in C#, use .NET’s HashSet<T> directly.
What’s the difference between JavaScript Set union and intersection?
Use union for an inclusive list: an ID can be in either input or both. Use intersection for a common list: an ID must be in both. The downloaded program labels these all and kept. It converts the result sets to arrays with spread syntax ([...set]) so their members can be checked and printed.
Union starts with the receiver’s IDs in order, then adds new IDs from the argument. It doesn’t sort: 103 appears after 104 in the full program. Intersection can iterate the smaller side, so do not assume its output always follows the receiver’s insertion order. For a report that requires sorted IDs, sort the result separately from finding the matches.
How do you find the difference between two JavaScript sets?
difference is directional. In the downloaded example, before.difference(after) contains only 101, which was removed. after.difference(before) contains only 103, which was added. symmetricDifference gives both, leaving out any ID that occurs in both sets. If the report needs to say which IDs to add and which to remove, keep the two directional results instead.
Can you use JavaScript Set intersection with arrays?
Wrap each array with new Set(...) first, as the snippet at the top does. This compares values without coercing their types: number 102 and string '102' are different keys. Objects match by identity, so separately created records with the same fields don’t match. Compare the ID values rather than whole records if the two lists come from separate reads of the same data.
JavaScript set composition requires the receiver to be a set, but the argument may be a set-like object with size, has, and keys. That’s why the map in the variation works and a bare array doesn’t. A set also discards multiplicity: use it for membership, not for counting how many times an ID appears in a list.