在JavaScript中访问以哈希(#)开头的API元素?

时间:2017-12-09 20:12:55

标签: javascript

我正在玩个人项目的Last.FM API。我想做的其中一件事就是抓住艺术家的形象。这是他们在艺术家对象中呈现的方式:

{
name: "Eels",
playcount: "8067",
mbid: "14387b0f-765c-4852-852f-135335790466",
url: "https://www.last.fm/music/Eels",
streamable: "0",
image: [
{
#text: "https://lastfm-img2.akamaized.net/i/u/34s/6dc93c3bba454f6aa0528dedf9a4b3ef.png",
size: "small"
},
{
#text: "https://lastfm-img2.akamaized.net/i/u/64s/6dc93c3bba454f6aa0528dedf9a4b3ef.png",
size: "medium"
},
{
#text: "https://lastfm-img2.akamaized.net/i/u/174s/6dc93c3bba454f6aa0528dedf9a4b3ef.png",
size: "large"
},
{
#text: "https://lastfm-img2.akamaized.net/i/u/300x300/6dc93c3bba454f6aa0528dedf9a4b3ef.png",
size: "extralarge"
},
{
#text: "https://lastfm-img2.akamaized.net/i/u/300x300/6dc93c3bba454f6aa0528dedf9a4b3ef.png",
size: "mega"
}
],
@attr: {
rank: "1"
}
},

以下是我在功能中尝试显示内容的内容:

topFive.forEach(function(artist,index){

var artistPhoto = document.createElement('img');
var artistName = document.createElement('p');
var artistPlays = document.createElement('p');

for (var picture of artist.image) {
  console.log(picture.text);
  console.log(picture.#text); // (doesn't work)
}

我现在只是控制台记录,看看能否抓住它。如果我是console.log“picture”,它会给我一个这样的对象:

{#text: "https://lastfm-img2.akamaized.net/i/u/34s/6dc93c3bba454f6aa0528dedf9a4b3ef.png", size: "small"}

我不清楚如何访问在其前面有哈希的对象键

1 个答案:

答案 0 :(得分:1)

只需使用索引器picture['#text']