最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using an asterisk* as a key in an object - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

You 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]['*']);
发布评论

评论列表(0)

  1. 暂无评论