While working in javascript, there is often a requirement to determine if a key exists in a particular object. This article demonstrates easy ways to check if a key exists in a javascript object using different methods and example illustrations.
Table of Contents:
- Check if key exists in javascript Object using in operator
- Check if key exists in javascript Object using hasOwnProperty()
- Check if key exists in javascript Object using some()
- Check if key exists in javascript Object using indexOf()
Check if key exists in object using in operator
Javascript’s in operator will return true if a particular property is present in a specified object.
Example:-
Check if the key ‘type’ and ‘quantity’ exist in the object apparels ={type:”pants”, colour:”red”, material:”cotton”}
Frequently Asked:
Code:-
function keyExists(_object, _key) { if(_key in _object) {console.log("Key Exists")} else{ console.log("Key Does Not Exist") } } //usage let apparels ={type:"pants", colour:"red", material:"cotton"}; keyExists(apparels, 'type'); keyExists(apparels, 'quantity');
Output:-
Key Exists Key Does Not Exist
Explanation:-
- In the above code, function keyExists(_object, _key) is used to find if a key exists in the object.
- Within the method, if condition uses the in operator to check if the _key (second argument) exists in the _object (first argument).
Check if key exists in object using hasOwnProperty()
Javascript’s hasOwnProperty() method will return true if a particular property is found within the object as its own property else will return false.
Example:-
Check if the key ‘type’ and ‘quantity’ exist in the object apparels ={type:”pants”, colour:”red”, material:”cotton”}
Code:-
function keyExists(_object, _key) { if(_object.hasOwnProperty(_key)) { console.log("Key Exists")} else{ console.log("Key Does Not Exist") } } //usage let apparels ={type:"pants", colour:"red", material:"cotton"}; keyExists(apparels, 'type'); keyExists(apparels, 'quantity');
Output:-
Key Exists Key Does Not Exist
Explanation:-
- In the above code, function keyExists(_object, _key) is used to find if a key exists in the object.
- Within the method, if condition uses the hasOwnProperty() method to check if the _key (second argument) exists in the _object (first argument).
Check if key exists in object using some()
Javascript’s some() method will test a callback function on all the elements of the calling array and returns true if it finds the element for which the callback function returns true.
Example:-
Check if the key ‘type’ and ‘quantity’ exist in the object apparels ={type:”pants”, colour:”red”, material:”cotton”}
Code:-
function keyExists(_object, _key) { var isKeyPresent = Object.keys(_object).some(value => value == _key); console.log(isKeyPresent); } //usage let apparels ={type:"pants", colour:"red", material:"cotton"}; keyExists(apparels, 'type'); keyExists(apparels, 'quantity');
Output:-
true false
Explanation:-
- In the above code, function keyExists(_object, _key) is used to find if a key exists in the object.
- Within the method, all the keys of the object are extracted into an array using Object.keys(_object).
- On the array of keys, some() method is applied to check if any element of the array of keys is equal to _key (second argument).
Note that some() can be used from ECMA Script 6 onwards.
Check if key exists in object using indexOf()
Javascript’s indexOf() method will return the first index at which an element is found in the array. If the element does not exist then, -1 is returned.
Example:-
Check if the key ‘type’ and ‘quantity’ exist in the object apparels ={type:”pants”, colour:”red”, material:”cotton”}
Code:-
function keyExists(_object, _key) { if(Object.keys(_object).indexOf(_key) === -1) { console.log("Key Does Not Exist")} else{ console.log("Key Exists") } } //usage let apparels ={type:"pants", colour:"red", material:"cotton"}; keyExists(apparels, 'type'); keyExists(apparels, 'quantity');
Output:-
Key Exists Key Does Not Exist
Explanation:-
- In the above code, function keyExists(_object, _key) is used to find if a key exists in the object.
- Within the method, all the keys of the object are extracted into an array using Object.keys(_object).
- On the array of keys indexOf(_key) method is applied. If the _key (second argument) is found then its first index is returned. Else -1 is returned, and the message is printed on the console accordingly.
Read More:
- Javascript: Insert an item to array at specific index
- Javascript: Check if an array is empty
- Javascript: Convert array to string (4 ways)
I hope this article helped you to check if a key exists in the javascript object. Good Luck !!!