Array/Since
1.38.0difference
Returns a new array containing the elements from the first array that are not included in the second array.
difference(array, values):T[]Parameters
arrayT[]requiredThe array whose elements are checked against the values array.
valuesT[]requiredThe array of values to exclude from the result.
Returns
T[]A new array containing elements from the first array that are not included in the values array.
Examples
01
Basic usage
Find the elements that exist in the first array but not in the second array.
TypeScript
import { difference } from "@cyref/js";
const numbers = [1, 2, 3, 4, 5];
const excluded = [2, 4];
const result = difference(numbers, excluded);
console.log(result);
// [1, 3, 5]02
With strings
The utility can also compare arrays of strings.
TypeScript
import { difference } from "@cyref/js";
const languages = ["JavaScript", "TypeScript", "Python"];
const excluded = ["Python"];
const result = difference(languages, excluded);
console.log(result);
// ["JavaScript", "TypeScript"]03
No matching values
If none of the elements from the first array are included in the second array, all elements are returned.
TypeScript
import { difference } from "@cyref/js";
const numbers = [1, 2, 3];
const excluded = [4, 5, 6];
const result = difference(numbers, excluded);
console.log(result);
// [1, 2, 3]Built with@cyref/js·Official documentation