最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - How to check if key exists in the data from the server in javascript? - Stack Overflow

programmeradmin1浏览0评论

I get some JSON values from the server but I don't know if there will be a particular field or not.

Sometimes i get the response in this form

{
 "regatta_name":"ProbaRegatta",
  "country":"Congo",
  "status":"invited"
}

Where as sometimes i get reponse in this form

{
 "regatta_name":"ProbaRegatta",
  "country":"Congo",
  "status":"invited",
   "vehicle": "yes"
}

How to check if vehicle key exists in the javascript object.?

I get some JSON values from the server but I don't know if there will be a particular field or not.

Sometimes i get the response in this form

{
 "regatta_name":"ProbaRegatta",
  "country":"Congo",
  "status":"invited"
}

Where as sometimes i get reponse in this form

{
 "regatta_name":"ProbaRegatta",
  "country":"Congo",
  "status":"invited",
   "vehicle": "yes"
}

How to check if vehicle key exists in the javascript object.?

Share Improve this question edited Nov 30, 2017 at 16:27 asosnovsky 2,2353 gold badges25 silver badges42 bronze badges asked Nov 30, 2017 at 16:24 prem chopraprem chopra 1071 gold badge3 silver badges12 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 7

Since you don't know if the object will be there or not then you can check with the response['prop'] syntax. You can also check with the dot syntax, as is being done above, but I prefer this way. I got into this habit because the response.prop syntax won't work in Typescript unless the property is clearly known by the compiler.

if (serverResponse && serverResponse['vehicle']) {
    // vehicle key exists
    // do your stuff 
} else {
    // vehicle key Does Not exist
    // do your stuff 
}

There are multiple ways to do the check like:

const obj = {
   "regatta_name":"ProbaRegatta",
   "country":"Congo",
   "status":"invited",
   "vehicle": "yes"
};
  • by using in operator: if("vehicle" in obj){ -- }
  • by using hasOwnProperty: if(obj.hasOwnProperty("vehicle")){ -- }
  • by checking: if(obj["vehicle"]){ -- } or if(obj.vehicle){ -- }

JS objects are very easy to check, since if you try to access it and it doesn't exist, no error is thrown. So you can simply check like this:

let response = {
   "regatta_name":"ProbaRegatta",
   "country":"Congo",
   "status":"invited",
   "vehicle": "yes"
};

if(response.vehicle) { // or if(response["vehicle"])
    // do stuff
}

This works because if JS cannot find the property 'vehicle' in the object, it will fail this conditional. So if the property does exist, the code will enter the if block and you can use the data in the object.

So first, assume the objects are stored as follows:

obj1 = {
 "regatta_name":"ProbaRegatta",
  "country":"Congo",
  "status":"invited"
}

obj2 = {
 "regatta_name":"ProbaRegatta",
  "country":"Congo",
  "status":"invited",
  "vehicle": "yes"
}

To check simply do

if('vehicle' in obj1){
 console.log(true)
}

if('vehicle' in obj2){
 console.log(true)
}

The output you get will be false, true

Also, you can use hasOwnProperty if you want. I.e.

obj1.hasOwnProperty('vehicle') // false
obj2.hasOwnProperty('vehicle') // true

You might be needing

JsonObj.hasOwnProperty('key')

check this refrence link

发布评论

评论列表(0)

  1. 暂无评论