includes javascript object code example
Example 1: javascript array contains
var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red");
var hasYellow = colors.includes("yellow");
Example 2: 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 3: javascript object includes
const person = {
first_name: "Sam",
last_name: "Bradley"
};
Object.values(person).includes("Bradley");
Example 4: .includes javascript
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 5: how to Check if an array contains an object in javascript
const myArrayObj = [{
'username': 'Player 1',
'email': 'john.doe@example.com'
}, {
'username': 'Player 2',
'email': 'jane.doe@example.com'
}];
const isEqual = (first, second) => {
return JSON.stringify(first) === JSON.stringify(second);
}
const result = myArrayObj.some(e => isEqual(e, {
'username': 'Player 1',
'email': 'john.doe@example.com'
}));
console.log(result);
Example 6: check if property has value in array javascript
const magenicVendorExists = vendors.reduce((accumulator, vendor) => (accumulator||vendor.Name === "Magenic"), false);