How to Check If Key Exists in JSON Object using JQuery?
When you require to check if key exists or not in json object in javascript. we can easily check jquery json object key exists or not using hasOwnProperty. jquery json provide several method for getting key value or check if key exists etc.
In this example we will use hasOwnProperty method of json object that will help to check if key exists or not in jquery.
if(myObject.hasOwnProperty('name')){
console.log('Key is exist in Object!');
}
hasOwnProperty return true if key is exists and return false if key is not exists on given javascript json. So let's see simple example that will help you how you can use.
Example:
<!DOCTYPE html>
<html>
<head>
<title>How to check if key exists in json object in jquery? - ItSolutionStuff.com</title>
</head>
<body>
<script type="text/javascript">
var myObject = {'id': 1, 'name': 'Paresh', 'city': 'Rajkot'};
if(myObject.hasOwnProperty('name')){
console.log('Key is exist in Object!');
}else{
console.log('Key is not exist in Object!');
}
</script>
</body>
</html>
I hope it can help you....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- JQuery Open Link in New Window Example
- How to Get Selected Radio Button Value in JQuery?
- How to Split String to Array By Comma using JQuery?
- How to Only Allow Numbers in a Text Box using jQuery?
- How to Disable F5 Refresh Button using JQuery?
- How to Disable Browser Back Button using JQuery?
- JQuery Disable Right Click, Cut, Copy and Paste Example
- JQuery Rotate Image Animation Example Code
- How to Remove Empty or Null Values from Array in JQuery?
- How to Remove Duplicate Object from Array in JQuery?
- How to Remove Duplicate Value from Array in JQuery?
- How to Remove All Spaces from String in JQuery?
- How to Parse JSON in JQuery Ajax?