Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

javascript - 获取ReferenceError:外观未定义(Getting ReferenceError: appearance is not defined)

I am trying to access an super hero api and for some of the data it keeps saying that the data is undefined.(我正在尝试访问超级英雄api,对于某些数据,它一直在说数据未定义。)

 var html = "";
$.each(data.results,function(i,results){
html += "<li> first appearance: " + results.biography.first-appearance +"</li>";
// it is not showing anything with a "-" in but the rest are working fine 

 })
 $("#Results").html(html);
  ask by Super_Fly translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the same way that object keys with hyphens in them need to be quoted, when you're accessing the key you need to use square bracket notation with quotes:(以与它们之间带有连字符的对象键相同的方式进行引用,在访问键时,需要使用带引号的方括号表示法:)

 const a = [{ biography: 'a', 'first-appearance': 'b' }]; console.log(a[0]['first-appearance']) 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...