我正在尝试访问 super 英雄 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);
最佳答案
就像需要引用带有连字符的对象键一样,当您访问该键时,您需要使用带引号的方括号表示法:
const a = [{ biography: 'a', 'first-appearance': 'b' }];
console.log(a[0]['first-appearance'])
关于javascript - 获取引用错误: appearance is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59112057/