Check if all array elements are equal to a given value
JavaScript version
const isEqual = (arr, value) => arr.every((item) => item === value);
// Or
// Ends earlier for false arrays
const isEqual = (arr, value) => !arr.some((item) => item !== value);
TypeScript version
const isEqual = <T,_>(arr: T[], value: T): boolean => arr.every((item) => item === value);
// Or
const isEqual = <T,_>(arr: T[], value: T): boolean => !arr.some((item) => item !== value);
Examples
isEqual(['foo', 'foo'], 'foo'); // true
isEqual(['foo', 'bar'], 'foo'); // false
isEqual(['bar', 'bar'], 'foo'); // false