es6 check if item in array code example
Example 1: javascript array contains
var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red");
var hasYellow = colors.includes("yellow");
Example 2: check value exist in array javascript
[1, 2, 3].includes(2);
[1, 2, 3].includes(4);
[1, 2, 3].includes(1, 2);
Example 3: 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)
Example 4: javascript check if array is in array
var array = [1, 3],
prizes = [[1, 3], [1, 4]],
includes = prizes.some(a => array.every((v, i) => v === a[i]));
console.log(includes);