includes javascript object code example

Example 1: javascript array contains

var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red"); //true
var hasYellow = colors.includes("yellow"); //false

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) // ['hi' 'bye' 'okay]

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) // ['hi' 'bye' 'okay]

Example 5: how to Check if an array contains an object in javascript

// To check if an array contains an Object

const myArrayObj = [{
    'username': 'Player 1',
    'email': 'john.doe@example.com'
}, {
    'username': 'Player 2',
    'email': 'jane.doe@example.com'
}];

// Create a helper function to compear the objects
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); // true

Example 6: check if property has value in array javascript

const magenicVendorExists =  vendors.reduce((accumulator, vendor) => (accumulator||vendor.Name === "Magenic"), false);