Check If Key Exists In Array JavaScript

Check If Key Exists In Array JavaScript | An Object in JavaScript is an unordered collection of key-value pairs. A key is also called a property. Property has a string value, if a non-string value is given it is stringified. The value of a property can be of any data type, a string, a number, an array, or even a function.

On the other hand, an array is an ordered collection of values. Each value can be called an element. Every element has an index value, which is used to identify the position of that element. An array can store elements of any datatype, like strings, numbers, and functions. A single array can have multiple data types stored in it.

There are several methods by which one can check if the key exists in an array of objects. Consider the following examples of object and array respectively, using those we will try to understand the different methods.

Object:

let person = {
    name: 'John',
    age: 20,
    gender: 'Male'
};

Array:

let num = [1, 2, 3, 7, 8, 9];

Check If Value Exists In Array JavaScript

let num = [1, 2, 3, 7, 8, 9];
console.log(num.some(value => value === 1));
console.log(num.some(value => value === 9));
console.log(num.some(value => value === 10));

Output:-

true
true
false

Check Key Exist in Object Using the ‘in’ Operator

let person = {
    name: 'John',
    age: 20,
    gender: 'Male'
};

console.log('name' in person);

Output:-

true

let person = {
    name: 'John',
    age: 20,
    gender: 'Male'
};

console.log('age' in person);
console.log('Male' in person);

Output:-

true
false

Check Key Exist in Object Using the hasOwnProperty()

The hasOwnProperty() method is used to determine whether the object has the given property as its property.

let person = {
    name: 'John',
    age: 20,
    gender: 'Male'
};

console.log(person.hasOwnProperty('name'));
console.log(person.hasOwnProperty('Male'));
console.log(person.hasOwnProperty(0));

Output:-

true
false
false

Check If Key Exists In Array JavaScript Using the hasOwnProperty()

Using hasOwnProperty() method we can check whether this array has an element at the given index or not? For example num.hasOwnProperty(5) checks whether the “num” array has an element at the 5th index or not, if it has an element at the 5th index then it returns true else it returns false.

let num = [1, 2, 3, 7, 8, 9];
console.log(num.hasOwnProperty(0));
console.log(num.hasOwnProperty(1));
console.log(num.hasOwnProperty(5));
console.log(num.hasOwnProperty(6));
console.log(num.hasOwnProperty(10));
console.log(num.hasOwnProperty('Male'));

Output:-

true
true
true
false
false
false

Using the Object.keys() Method

The Object.keys() method returns an array whose elements are keys of the object passed in it. We can query this array to find out whether the key exists or not

There are two ways in which the Object.keys() method can be used to find the index of a key in an object and those methods can be used for arrays as well.

Using some() with Object.keys()

let person = {
    name: 'John',
    age: 20,
    gender: 'Male'
};
console.log(Object.keys(person).some(key => key === 'name'));
console.log(Object.keys(person).some(key => key === 'role'));

Output:-

true
false

Using indexOf() along with Object.keys()

The indexOf() method for an object should only be used to find the existence of a key. As an object is unsorted, the index of the key returned by indexOf() might not always be the position of that key.

let person = {
    name: 'John',
    age: 20,
    gender: 'Male'
};
console.log(Object.keys(person).indexOf('name'));
console.log(Object.keys(person).indexOf('role'));

Output:-

0
-1

let num = [1, 2, 3, 7, 8, 9];
console.log(num.indexOf(3));
console.log(num.indexOf(9));

Output:-

2
5

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *