diginext-utils
Version:
README.md
29 lines • 863 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.difference = difference;
/**
* Creates an array of values from the first array that are not present in the second array.
*
* @template T - The type of elements in the arrays
* @param array1 - Array to check
* @param array2 - Values to exclude
* @returns A new array with values from array1 not in array2
*
* @example
* ```ts
* difference([1, 2, 3, 4], [2, 4]); // [1, 3]
* difference(['a', 'b', 'c'], ['b']); // ['a', 'c']
* difference([1, 2], [3, 4]); // [1, 2]
* ```
*/
function difference(array1, array2) {
if (!Array.isArray(array1)) {
return [];
}
if (!Array.isArray(array2)) {
return [...array1];
}
const set2 = new Set(array2);
return array1.filter((item) => !set2.has(item));
}
//# sourceMappingURL=difference.js.map