我怎么看大JSON对象是否包含值?

Har*_*ldo 21 javascript jquery json

我正在使用PHP来json编码一个大规模的多维事件数组,所以我得到这样的东西:

var ents = {"7":{"event_id":"7","nn":"The Whisky Drifters","nn_url":"the-whisky-drifters",
  "venue":"The Grain Barge","date_num":"2010-06-11","date_txt":"Friday 11th June",
  "gig_club":"1","sd":"A New Acoustic String Band...","ven_id":"44",
  "art":0},"15":{"event_id":"15","nn":"Bass Kitchen","nn_url":"bass-kitchen",
  "venue":"Timbuk2","date_num":"2010-06-11","date_txt":"Friday 11th June",
  "gig_club":"2","sd":"Hexadecimal \/ DJ Derek \/ Id","ven_id":"21",
  "art":1},
Run Code Online (Sandbox Code Playgroud)

第一个维度是id,请参阅

var ents = {"7":{
Run Code Online (Sandbox Code Playgroud)

因此,可以在不检查嵌套对象的情况下获取ID ...

检查我的JSON是否包含id的最快,最有效的方法是什么?

CMS*_*CMS 47

您可以使用以下hasOwnProperty方法:

if (ents.hasOwnProperty('7')) {
  //..
}
Run Code Online (Sandbox Code Playgroud)

此方法检查对象是否包含指定的属性,而不管其值如何.

in运算符更快,因为它不检查继承的属性.