I'm getting an error trying to use an asterisk as a key reference of an object. I've tried formatting different ways, but always get the same error:
SyntaxError: missing name after . operator
Here is my code with an object rendered from the wikipedia api...
The line in question is:
console.log(shortcut.langlinks[index].*);
var wp = {
"query":{
"pages":{
"3823":{
"pageid":3823,
"ns":0,
"title":"Binary",
"extract":"<p><b>Binary</b> means <i>posed 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.");
}
}
How do I format my code to get the asterisk to display like a key value? Thanks.
I'm getting an error trying to use an asterisk as a key reference of an object. I've tried formatting different ways, but always get the same error:
SyntaxError: missing name after . operator
Here is my code with an object rendered from the wikipedia api...
The line in question is:
console.log(shortcut.langlinks[index].*);
var wp = {
"query":{
"pages":{
"3823":{
"pageid":3823,
"ns":0,
"title":"Binary",
"extract":"<p><b>Binary</b> means <i>posed 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.");
}
}
How do I format my code to get the asterisk to display like a key value? Thanks.
Share Improve this question asked Jul 27, 2013 at 22:33 LekeLeke 8933 gold badges15 silver badges28 bronze badges3 Answers
Reset to default 5You can use brackets as well:
shortcut.langlinks[index]['*']
When you want to access a property whose name is also a valid name for an identifier you can use the dot syntax: shortcut.langlinks
(langlinks
is a valid identifier name).
When the property name is not a valid identifier name, you must use the angle bracket syntax instead: langlinks[index]["*"]
(*
is not a valid identifier name because it does not start with "$", "_", or any Unicode character that is classed as a letter).
You can use:
console.log(shortcut.langlinks[index]['*']);