Borislav Hadzhiev
Tue Feb 15 2022·3 min read
Photo by Brooke Cagle
Use the includes()
method to check if an array contains a value in
TypeScript, e.g. if (arr.includes('two')) {}
. The includes
method will
return true
if the value is contained in the array and false
otherwise.
const arr: string[] = ['one', 'two', 'three']; if (arr.includes('two')) { console.log('✅ two is contained in array'); } else { console.log('⛔️ two is NOT contained in array'); }
The Array.includes method performs a case-sensitive check whether a value is contained in an array.
To perform a case insensitive check whether a string is contained in an array:
Array.find()
method.Array.find
method returns the first array element that satisfies the
condition.const arr: string[] = ['one', 'two', 'three']; const str = 'TWO'; const found = arr.find((element) => { return element.toLowerCase() === str.toLowerCase(); }); console.log(found); // 👉️ "two" if (found !== undefined) { console.log('✅ the string is contained in the array'); } else { console.log('⛔️ the string is NOT contained in the array'); }
The function we passed to the Array.find method gets invoked with each element in the array until it returns a truthy value or iterates over all elements.
In the code example, the case insensitive check for the string succeeded and
Array.find
returned the corresponding array element.
Array.find
return a falsy value, then the method returns undefined
.You could also use the Array.find
method to check if an object
is contained
in an array.
To check if a TypeScript array contains an object:
Array.find()
method.true
if it isArray.find
will return the object if the conditional check is satisfied at
least onceconst arr: { id: number; name: string }[] = [ { id: 1, name: 'Tom' }, { id: 2, name: 'Alfred' }, { id: 3, name: 'Fred' }, ]; const found = arr.find((obj) => { return obj.id === 2; }); console.log(found); // 👉️ {id: 1, name: 'Alfred'} if (found !== undefined) { console.log('✅ the object is contained in the array'); } else { console.log('⛔️ the object is NOT contained in the array'); }
Array.find
method gets called with each object in the array until it returns a truthy value or iterates over all the elements.On each iteration, we check if the object's id
is equal to 2
and if it is,
the condition is met and the object is returned from the find
method.
An alternative approach is to use the Array.some method.
To check if a TypeScript array contains an object:
Array.some()
method.true
if it is.Array.some
will return true
is the conditional check is satisfied at
least once.const arr: { id: number; name: string }[] = [ { id: 1, name: 'Tom' }, { id: 2, name: 'Alfred' }, { id: 3, name: 'Fred' }, ]; const result = arr.some((obj) => { return obj.id === 2; }); console.log(result); // 👉️ true if (result) { console.log('✅ the object is contained in the array'); } else { console.log('⛔️ the object is NOT contained in the array'); }
The Array.some()
method returns a boolean result - true
if the condition is
satisfied at least once and false
otherwise.
This might be the more intuitive approach if you don't need to get the value of the object.