JavaScript check if value exists in array of objects

Here's a Code Recipe to check if a #JavaScript array contains a value. You can use the new array

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
3 method πŸ˜‹ For older browsers and IE, you can use
const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
4 πŸ‘

const array = ['πŸ₯—', 'πŸ”', '🍰'];

// Modern Browser
array.includes('🍰'); // true

// Older Browser
array.indexOf('🍰') !== -1; // true

const symbol = Symbol('symbol'); const array = [ 'string', 200, 0, undefined, null, symbol ]; 3 with other primitive types

Besides strings,

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
3 also works great with other primitive types.

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];

Using

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
3

array.includes('string'); // true
array.includes(200); // true
array.includes(0); // true
array.includes(undefined); // true
array.includes(null); // true
array.includes(symbol); // true

Using

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
4

array.indexOf('string') !== -1; // true
array.indexOf(200) !== -1; // true
array.indexOf(0) !== -1; // true
array.indexOf(undefined) !== -1; // true
array.indexOf(null) !== -1; // true
array.indexOf(symbol) !== -1; // true

Caveats of const symbol = Symbol('symbol'); const array = [ 'string', 200, 0, undefined, null, symbol ]; 4

So far, I have shown you values where both

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
3 and
const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
4 work interchangeably. However, there is one value, where they differ 🀭

const array = [NaN];

// βœ…
array.includes(NaN); // true

// 😱
array.indexOf(NaN) !== -1; // false

Checking for Array of Objects using array.includes('string'); // true array.includes(200); // true array.includes(0); // true array.includes(undefined); // true array.includes(null); // true array.includes(symbol); // true 2

For a more versatile solution that works on other data types, you may want to use

array.includes('string'); // true
array.includes(200); // true
array.includes(0); // true
array.includes(undefined); // true
array.includes(null); // true
array.includes(symbol); // true
3 instead.

".some()": tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

const array = ['πŸ₯—', 'πŸ”', '🍰'];

array.some(food => food === '🍰'); // true

This method is ideal for an array of objects.

const array = [{ name: 'js' }, { name: 'css' }];

array.some(code => code.name === 'js'); // true
array.some(code => code.name === 'πŸ€–'); // false

In a previous code note, I talked about a quick & dirty way to check

array.includes('string'); // true
array.includes(200); // true
array.includes(0); // true
array.includes(undefined); // true
array.includes(null); // true
array.includes(symbol); // true
4 using
array.includes('string'); // true
array.includes(200); // true
array.includes(0); // true
array.includes(undefined); // true
array.includes(null); // true
array.includes(symbol); // true
5.

How to Compare 2 Objects in JavaScript

Taking that concept, we can also use it to compare object element in an array like this:

const array = [{ name: 'js' }, { name: 'css' }];

array.some(code => JSON.stringify(code) === JSON.stringify({ name: 'css' }));
// true

Case Sensitive

Both

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
3 and
const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
4 are case sensitive:

const array = ['SAMANTHA'];

array.includes('samantha'); // false
array.indexOf('samantha') !== -1; // false

To make it case insensitive, you could consider changing the case of the array like so:

const array = ['SAMANTHA'];

const sameCaseArray = array.map(value => value.toLowerCase());
// ['samantha']

sameCaseArray.includes('samantha'); // true
sameCaseArray.indexOf('samantha') !== -1; // true

But if you were using

array.includes('string'); // true
array.includes(200); // true
array.includes(0); // true
array.includes(undefined); // true
array.includes(null); // true
array.includes(symbol); // true
3, you can do it in one line:

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
0

Browser Support

Support for

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
3 is really good for all modern browsers. However, if you need IE or older browser, you will need to use
const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
4.

Can I use?

Community Input

  • @lolinoid:

    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    1 > @prvnbist That's a method DOM Nodes, most known example for it would be getting a list of classnames which will be a node list then you can use contain method to see if it has a classname. Or you can convert it to an array and then use includes method

  • You can use the

    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    2 operator to loop over an object to check if a specific property key exists. (Knowledge shared by @fvbix)

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
1

  • @pixelfixer: And if you want the value returned, you can use

    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    3

  • Performance test > array indexOf vs includes. Thanks @equiman:

  • @smokku: You can avoid all these

    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    4 comparison using
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    5 operator

const symbol = Symbol('symbol');

const array = [
  'string',
  200,
  0,
  undefined,
  null,
  symbol
];
2

  • @danvc:

    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    6. The bitwise
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    5 operator will return a truthy value for anything but
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    8. Negating it is as simple as doing
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    9.

  • @smokku: Bitwise

    const array = [NaN];
    
    // βœ…
    array.includes(NaN); // true
    
    // 😱
    array.indexOf(NaN) !== -1; // false
    
    0 gives you the opposite of the number, but we use two's complementsystem to avoid having a
    const array = [NaN];
    
    // βœ…
    array.includes(NaN); // true
    
    // 😱
    array.indexOf(NaN) !== -1; // false
    
    1 and
    const array = [NaN];
    
    // βœ…
    array.includes(NaN); // true
    
    // 😱
    array.indexOf(NaN) !== -1; // false
    
    2. So the negative numbers are shifted by one - where
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    8 takes place of
    const array = [NaN];
    
    // βœ…
    array.includes(NaN); // true
    
    // 😱
    array.indexOf(NaN) !== -1; // false
    
    2. When we negate the
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    8 we get
    const array = [NaN];
    
    // βœ…
    array.includes(NaN); // true
    
    // 😱
    array.indexOf(NaN) !== -1; // false
    
    1, which is the only falsey value. All other indices we can get of
    const array = [NaN];
    
    // βœ…
    array.includes(NaN); // true
    
    // 😱
    array.indexOf(NaN) !== -1; // false
    
    7 will give you a truthy (non-zero) value when passed through bitwise not
    array.indexOf('string') !== -1; // true
    array.indexOf(200) !== -1; // true
    array.indexOf(0) !== -1; // true
    array.indexOf(undefined) !== -1; // true
    array.indexOf(null) !== -1; // true
    array.indexOf(symbol) !== -1; // true
    
    5.

  • @smokku: If you are interested in diving this deep, I highly recommend a book by Charles Petzold called "Code: The Hidden Language of Computer Hardware and Software". It is a fun read, that will guide you from the basics of code design (like Morse, Braile, etc.) through electrical switches and flashing lightbulbs, telegraph, electronic gates and flip-flops, up to the simple CPU level.

    How do you check if a value exists in an array of objects in JavaScript?

    You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

    How do you check if an array of objects includes a value?

    Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.

    How to check if value exists in object JavaScript?

    _.has() method is used to check whether the path is a direct property of the object or not. It returns true if the path exists, else it returns false.

    How to check if all objects in array have same property value?

    In order to check whether every value of your records/array is equal to each other or not, you can use this function. allEqual() function returns true if the all records of a collection are equal and false otherwise. let's look at the syntax… const allEqual = arr => arr.