How to check if key exists in json object in jquery?
In this post we will give you information about How to check if key exists in json object in jquery?. Hear we will give you detail about How to check if key exists in json object in jquery?And how to use it also give you demo for it if it is necessary.
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….
Hope this code and post will helped you for implement How to check if key exists in json object in jquery?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs