Filter out values found in any exclusion array
A mailing tool builds the recipient list for a campaign from all subscribers, then drops anyone who unsubscribed, anyone who bounced, and anyone on the legal hold list. Each of those lists comes from a different service.
Implement excludeValues(source, ...exclusions). It returns a new array with the items of source that do not appear in any of the exclusion arrays.
excludeValues(source, ...exclusions) // => new array
source in their original order, including duplicates: if a kept value appears three times in source, it appears three times in the result.source (a new array).Set and Array.prototype.includes use: NaN matches NaN, 0 matches -0, and objects match only the same reference.source is not an array, return []. Exclusion arguments that are not arrays (for example undefined from an API that failed) are ignored.source and m is the total length of the exclusion arrays.const subscribers = ['ana@x.io', 'bo@x.io', 'cy@x.io', 'di@x.io'];
const unsubscribed = ['bo@x.io'];
const bounced = ['di@x.io', 'zed@x.io'];
excludeValues(subscribers, unsubscribed, bounced); // => ['ana@x.io', 'cy@x.io']
excludeValues([3, 1, 3, 2, 1], [1]); // => [3, 3, 2]
excludeValues([NaN, 0, 7], [NaN, -0]); // => [7]
excludeValues([4, 5], undefined, [5]); // => [4]
excludeValues(null, [1]); // => []