I'm trying to write a function that can check whether a specified value exists in an array and also whether a value other than what is specified exists in an array. I'm looking for a modern solution, no backwards compatibility is required.
For example:
const array = [1,1,1,2,3];
// this checks whether a value exists
const valExists = (array, value) => array.includes(value);
valExists(array,1); // returns true
Now, how do I check whether anything other than 1 exists?
I've tried manipulating the function parameter value e.g:
valExists(array, !1); // returns false, i want 'true'
Solution
I've integrated the solution provided by my accepted answer as follows:
const array = [1,1,1,2,3];
const array2 = [1,1,1,1,1];
//this checks whether a value exists and also whether it is unique
function existUnique(array, value) { let a = array.includes(value); let b = array.every( e => e === value ); console.log(`${a}: ${b}`);};
The results are:
existUnique(array, 1); // true: false
existUnique(array2, 1); // true: true