how to Check if an array contains a string code example
Example 1: how to check if item is in list js
var myList=["a", "b", "c"];
mylist.includes("d")
Example 2: check if array contain the all element javascript
const myArray: number[] = [2, 4, 6, 8, 10, 12, 14, 16];
const elements: number[] = [4, 8, 12, 16];
function containsAll(arr: number[]) {
return (
arr.includes(elements[0]) &&
arr.includes(elements[1]) &&
arr.includes(elements[2]) &&
arr.includes(elements[3])
);
}
console.log(containsAll(myArray));
or you could use the following line:
function c2(arr: number[]) {
return elements.every((val: number) => arr.includes(val));
}
Example 3: how to Check if an array contains a string
const colors = ['red', 'green', 'blue'];
const result = colors.includes('red');
console.log(result);
const colors = ['Red', 'GREEN', 'Blue'];
const result = colors.map(e => e.toLocaleLowerCase())
.includes('green');
console.log(result);