includes on object javascript code example

Example 1: js does object contain value

// You can turn the values of an Object into an array.
// Then test that a value is present:

// This assumes, that the Object is not nested
// and the value is an exact match:

var obj = { a: 'test1', b: 'test2' };

// Note that an array can only have positive index's; so
// checking for a negative index is a super valid way for getting a
// boolean value as a result for the check.
if (Object.values(obj).indexOf('test1') > -1) {
   console.log('Value exists!');
}

Example 2: javascript object includes

const person = {
  first_name: "Sam",
  last_name: "Bradley"
};

Object.values(person).includes("Bradley");

Example 3: 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': '[email protected]'
}, {
    'username': 'Player 2',
    'email': '[email protected]'
}];

// 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': '[email protected]'
}));

console.log(result); // true