Example 1: javascript array contains
var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red"); //true
var hasYellow = colors.includes("yellow"); //false
Example 2: js array includes
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
Example 3: javascript includes
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// output: true
Example 4: array includes
let storyWords = ['extremely', 'literally', 'actually', 'hi', 'bye', 'okay']
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
let betterWords = storyWords.filter(function(word) {
return !unnecessaryWords.includes(word);
});
console.log(betterWords) // ['hi' 'bye' 'okay]
Example 5: 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));
}