尝试使用星号作为对象的关键引用时出现错误。我试过格式化不同的方式,但总是得到同样的错误:
SyntaxError: missing name after . operator
这是我的代码,其中包含从 wikipedia api 呈现的对象...
有问题的行是:
console.log(shortcut.langlinks[index].*);
var wp = {
"query":{
"pages":{
"3823":{
"pageid":3823,
"ns":0,
"title":"Binary",
"extract":"<p><b>Binary</b> means <i>composed of two pieces or two parts</i> and may refer to:</p>\n\n",
"links":[{
"ns":0,"title":"Binary-coded decimal"},{
"ns":0,"title":"Binary (Assemblage 23 song)"},{
"ns":0,"title":"Binary code"}],
"langlinks":[{
"lang":"de","*":"Bin\u00e4r"},{
"lang":"fr","*":"Binaire"},{
"lang":"ur","*":"\u062a\u062b\u0646\u06cc\u06c1"}]
}
}
}
};
var page_key = Object.keys( wp['query']['pages'])[0];
var shortcut = wp['query']['pages'][page_key];
function translation() {
if (shortcut.langlinks.length > 0){
for (var index in shortcut.langlinks){
if (shortcut.langlinks[index].lang == 'de'){
console.log(shortcut.langlinks[index].*);
}
}
} else {
console.log("There are no language links.");
}
}
如何格式化我的代码以使星号像键值一样显示?谢谢。