使用 $ 作为 JS 对象中的键?

发布于 2024-11-01 11:27:11 字数 2002 浏览 1 评论 0原文

示例:

console.log(myobject['media$group']);

输出未定义

console.log(myobject['media\$group']);

输出期望值

这是怎么回事?我在使用 youtube 的 API 时遇到了这个问题。

编辑:

我对你们很多人说它有效感到困惑,所以我会发布更多信息,因为也许我错过了一些愚蠢的东西。

这是我正在运行的代码:

console.log(feed);
console.log(feed["media$group"]["media$thumbnail"]);

这是从 firebug 剪切\粘贴的第一个日志中的对象:

author
    [Object { name={...}, uri={...}}]

category
    [Object { scheme="http://schemas.google.com/g/2005#kind", term="http://gdata.youtube.com/schemas/2007#video"}, Object { scheme="http://gdata.youtube.co...mas/2007/categories.cat", term="Tech", label="Science & Technology"}, Object { scheme="http://gdata.youtube.com/schemas/2007/keywords.cat", term="google"}, 10 more...]

content
    Object { $t="The Google Doodle team ...googleblog.blogspot.com", type="text"}

gd$comments
    Object { gd$feedLink={...}}

gd$rating
    Object { average=4.77813, max=5, more...}

id
    Object { $t="http://gdata.youtube.co.../api/videos/3NGSU2PM9dA"}

link
    [Object { rel="alternate", type="text/html", href="http://www.youtube.com/...A&feature=youtube_gdata"}, Object { rel="http://gdata.youtube.co...as/2007#video.responses", type="application/atom+xml", href="http://gdata.youtube.co...s/3NGSU2PM9dA/responses"}, Object { rel="http://gdata.youtube.co...emas/2007#video.related", type="application/atom+xml", href="http://gdata.youtube.co...eos/3NGSU2PM9dA/related"}, 2 more...]

media$group
    Object { media$category=[1], media$content=[3], more...}

published
    Object { $t="2011-04-14T00:31:33.000Z"}

title
    Object { $t="Charlie Chaplin Google Doodle", type="text"}

updated
    Object { $t="2011-04-15T23:14:58.000Z"}

yt$statistics
    Object { favoriteCount="2539", viewCount="0"}

这是第二个 console.log 中的错误:

feed.media is undefined
[Break On This Error] console.log(feed["media"]["media"]); 

Example:

console.log(myobject['media$group']);

outputs undefined

console.log(myobject['media\$group']);

outputs expected value

What's the deal? I ran into this when using youtube's API.

EDIT:

I'm confused by many of you saying it works, so I will post more info because perhaps I'm missing something stupid.

Here's the code I'm running:

console.log(feed);
console.log(feed["media$group"]["media$thumbnail"]);

Here's the object from the first log cut\pasted from firebug:

author
    [Object { name={...}, uri={...}}]

category
    [Object { scheme="http://schemas.google.com/g/2005#kind", term="http://gdata.youtube.com/schemas/2007#video"}, Object { scheme="http://gdata.youtube.co...mas/2007/categories.cat", term="Tech", label="Science & Technology"}, Object { scheme="http://gdata.youtube.com/schemas/2007/keywords.cat", term="google"}, 10 more...]

content
    Object { $t="The Google Doodle team ...googleblog.blogspot.com", type="text"}

gd$comments
    Object { gd$feedLink={...}}

gd$rating
    Object { average=4.77813, max=5, more...}

id
    Object { $t="http://gdata.youtube.co.../api/videos/3NGSU2PM9dA"}

link
    [Object { rel="alternate", type="text/html", href="http://www.youtube.com/...A&feature=youtube_gdata"}, Object { rel="http://gdata.youtube.co...as/2007#video.responses", type="application/atom+xml", href="http://gdata.youtube.co...s/3NGSU2PM9dA/responses"}, Object { rel="http://gdata.youtube.co...emas/2007#video.related", type="application/atom+xml", href="http://gdata.youtube.co...eos/3NGSU2PM9dA/related"}, 2 more...]

media$group
    Object { media$category=[1], media$content=[3], more...}

published
    Object { $t="2011-04-14T00:31:33.000Z"}

title
    Object { $t="Charlie Chaplin Google Doodle", type="text"}

updated
    Object { $t="2011-04-15T23:14:58.000Z"}

yt$statistics
    Object { favoriteCount="2539", viewCount="0"}

And here's the error from the second console.log:

feed.media is undefined
[Break On This Error] console.log(feed["media"]["media"]); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃兔兔 2024-11-08 11:27:11

'media\$group''media$group' 相等,因为 \$ 不是已知的转义序列,因此会变成 $。

因此,该字段只是在两次调用之间定义的。

'media\$group' and 'media$group' are equal since \$ is not a known escape sequence and thus turns into $.

So the field simply got defined between your two calls.

拥抱没勇气 2024-11-08 11:27:11

你的情况很有趣,我不知道为什么你需要用反斜杠转义 $ 字符,但这些是预期的输出。

var obj = {
    some$Key: 'someValue'
}

console.log(obj.some$Key); // someValue
console.log(obj['some$Key']); // someValue

Your case is interesting, I don't know why you required to escape $ character with a back slash, but these are outputs as expected.

var obj = {
    some$Key: 'someValue'
}

console.log(obj.some$Key); // someValue
console.log(obj['some$Key']); // someValue
仅此而已 2024-11-08 11:27:11

听起来像是服务器转义了字符串中的字符。该或其他一些字符串构建系统已转义 $ 字符,然后将您的对象设置为该值。

还有 "foo$bar" === "foo\$bar"

Sounds like the server escaping characters in your string. That or some other string building system has been escaping the $ character and then setting your object onto that value.

Also "foo$bar" === "foo\$bar"

故笙诉离歌 2024-11-08 11:27:11

效果很好。

a={'a':'test1', '

尝试这个 (jsfiddle)

:'yes'} console.log(a['

尝试这个 (jsfiddle)

]); // 'yes'

尝试这个 (jsfiddle)

It works fine.

a={'a':'test1', '

Try this (jsfiddle)

:'yes'} console.log(a['

Try this (jsfiddle)

]); // 'yes'

Try this (jsfiddle)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文