How to check whether a particular key exist in javascript object or array?



In JavaScript an object is found to be in the form of key value pairs. The keys of the objects are known as properties of the given object and is represented using a string. The property of the object can have a value which can be of any data type.

For example, if an employee object is created then it has the properties like employee name, employee id, employee age, salary etc. These are the properties of the employee object which are called keys. The values of these properties will be different for different employees.

In case of array, the key is the index and there will be a value at the index if the given index exists.

To check for the existence of key for an object or array in JavaScript can be done by using some functions and operators.

Using in operator

The in operator checks for the given key only for an object and returns a Boolean result i.e., ‘true’ if the object has the given key or ‘false’ if it doesn’t accordingly.

Syntax

The syntax of in operator is shown below.

For objects:
key in objectName //returns true if the object has the (key)objectProperty

Example

This example demonstrates the usage of in operator to check if key exists in Object −

let employee = { firstName: 'Mohammed', lastName: 'Abdul Rawoof', id : 1000, designation: 'Intern Software Engineer', salary : 18000 }; console.log("The given employee details are: ",employee) console.log("Is firstName present in employee:",'firstName' in employee); console.log("Is employee age present in employee:",'age' in employee) console.log("Is 18000 present in employee:", 18000 in employee)

In operator for arrays

In arrays, if a value exists at the given index, then the in operator will return true or else it returns false.

Syntax

For arrays:
Index in arrayName //returns true if the given index has a value

Example

This example demonstrates how to check if a key exists in arrays using in operator −

function getKeyArr(a,indx){ console.log("The given array with its length is:",a,a.length) if(indx in a){ console.log("The array has a key at the given index",indx) } else{ console.log("The array doesn't have a key at the given index",indx," as its array length is:",a.length) } } console.log("Checking the existance of a key in an array using hasOwnProperty()") getKeyArr([12,56,33,2,7],4) getKeyArr([12,56,33,2,7],8) getKeyArr([1,2,4,53,36,7,83,90,45,28,19],16)

Using hasOwnProperty() function

The function hasOwnProperty() will check for the existence of a key in the given object and returns true if the key is present or else it returns false. This function takes the key of the object as the parameter and returns the Boolean result accordingly.

Syntax

This is the syntax for the hasOwnProperty() function.

For object:
objectName.hasOwnPropert(key) //key is object property.

Example 3

This example demonstrates the usage of hasOwnProperty() to check for keys in object −

let employee = { emp_name: 'Abdul Rawoof', emp_id: 1000, role: 'Software Engineer', salary: 18000 }; console.log("The given employee details are:", employee) console.log("Checking the keys present in an object using hasOwnProperty:") function getEmpDet(str){ if(employee.hasOwnProperty(str)){ console.log("The given employee object has",str) } else{ console.log("The given employee object doesn't have",str) } } getEmpDet('emp_id') getEmpDet('salary') getEmpDet('designation')

hasOwnProperty() for Arrays

In arrays, if a value exists at the given index, then the in operator will return true or else it returns false. Whereas the hasOwnProperty() method can check if an index is available but not empty in the array.

Syntax

The syntax of hasOwnProperty() for arrays is seen below.

arrayName.hasOwnProperty(index)

Example

This example demonstrates how to check if a key exists in arrays using hasOwnProperty −

function getKeyArr(a,indx){ console.log("The given array with its length is:",a,a.length) if(a.hasOwnProperty(indx)){ console.log("The array has a key at the given index",indx) } else{ console.log("The array doesn't have a key at the given index",indx," as its array length is:",a.length) } } console.log("Checking the existance of a key in an array using hasOwnProperty()") getKeyArr([12,56,33,2,7],4) getKeyArr([12,56,33,2,7],8) getKeyArr([1,2,4,53,36,7,83,90,45,28,19],16)

Advertisements