【问题标题】:Can't get value from object key using .notation or bracket notation无法使用 .notation 或括号表示法从对象键中获取值
【发布时间】:2015-12-18 20:39:18
【问题描述】:
for(j=0;j<twitchUserArray.length;j++)
        {
          displayALL(twitchUserArray[j]);
        }

    function displayALL(person){
      console.log("Inside displayALL");
      console.log(person);
      console.log(person.logo);

     }

数组[9]

0:对象

game: ""
logo: "http: //static-cdn.jtvnw.net/jtv_user_pictures/freecodecamp-profile_image-f1b681380c0b0380-300x300 .png"
name: "freecodecamp"
status: null
twitchfeed: "http: //www.twitch.tv/freecodecamp"
__proto__: Object

1:对象

game: ""
logo: "http: //static-cdn.jtvnw.net/jtv_user_pictures/storbeck-profile_image-7ab13c2f781b601d-300x300 .jpeg"
name: "storbeck"
status: null
twitchfeed: "http: //www.twitch.tv/storbeck"
__proto__: Object

2:对象

game: ""
logo: null
name: "terakilobyte"
status: null
twitchfeed: "http: //www.twitch.tv/terakilobyte"
__proto__: Object

谁能告诉我在 displayALL 函数中获取键值的正确语法。该 for 循环中的每个索引都是一个对象。 Console.log(person) 在 chrome 中进行故障排除时向我显示包含所有键和值的对象,但我已经尝试过。表示法和方括号表示法,除了未定义之外什么都得不到

【问题讨论】:

  • codepen.io/RawleJuglal/pen/VeaKYm 这是 codepen,如果你想看的话。谢谢。
  • 您需要向我们展示该数组中的内容。
  • OK,添加了数组的前三个。将每个索引显示为单独的索引以及其中的内容。所有 9 个都有相同的键。感谢您的关注。

标签: object notation


【解决方案1】:

也许你应该看看一个人在 displayALL 中使用什么 typeof

   for(j=0;j<twitchUserArray.length;j++)
    {
      displayALL(twitchUserArray[j]);
    }

    function displayALL(person){
      console.log("Inside displayALL");
      console.log(person);
      console.log(typeof person);
 }

【讨论】:

  • console.log(typeof person);向我提出反对意见。这就是为什么我不确定为什么不能使用点符号来获取值的原因。你有什么不一样的吗?
  • 我已经做了一些进一步的 console.log 测试,函数 displayALL 不是问题。出于某种原因,console.log(twitchUserArray) 给了我我期望的对象数组。但是,如果我尝试运行一个 for 循环,甚至只是一个 for 循环,console.log(twitchUserArray[j]) 它将记录到控制台空对象。所以我不知道为什么对象中的所有数据都消失了。
  • 是的,您的问题在于回调等。您需要注意您的 Ajax 调用会发生什么。
猜你喜欢
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2016-04-02
  • 1970-01-01
相关资源
最近更新 更多