我正在获得这样的@attr 值:
item['@attr']['nowplaying']
当属性存在于 json 中时,这可以正常工作,但如果它不存在,我会收到错误消息:
Uncaught TypeError: Cannot read property 'nowplaying' of undefined
我怎样才能避免这个错误?
我正在获得这样的@attr 值:
item['@attr']['nowplaying']
当属性存在于 json 中时,这可以正常工作,但如果它不存在,我会收到错误消息:
Uncaught TypeError: Cannot read property 'nowplaying' of undefined
我怎样才能避免这个错误?
if (item['@attr']){
var nowPlaying = item['@attr']['nowplaying']);
}
Test to see if item['@attr']
has a value that can have properties before you try to access any properties of it.
if (item['@attr']) {
whatever(item['@attr']['nowplaying']);
}