简体   繁体   English

为什么当我尝试访问它的数组时,传递给第二个 function 的 object(包含一个数组)会给出一个空数组?

[英]Why does the object (containing an array) passed to second function, give an empty array when I try to access its array?

I am getting a response data .我得到一个响应数据 I pass it to another function and then log it.我将它传递给另一个 function 然后记录它。 The first time I log the entire object and get the desired output, but the second time I try to log the array inside that object, I get an empty array.第一次我记录整个 object 并获得所需的 output,但第二次我尝试在 object 中记录数组时,我得到一个空数组。 (I have cross checked that array is not empty). (我已经交叉检查了数组不为空)。

const f1 = () => {
  window.gapi.load('analytics', async function() {
    window.gapi.analytics.auth.authorize({
      'serverAuth': {
        'access_token': access_token
      }
    });
    const data = await new window.gapi.analytics.ViewSelector({
            container: 'view-selector-container'

    });
    data.execute();
    f2(data);
  });

}

const f2 = async function (data){
  console.log(data);
  console.log(data["zt"]["iH"]);
}

response1:回应1:

      {…}
​
Jd: Object { Lb: false, Zh: 1, Sw: 0, … }
​
Rb: Object { container: "view-selector-container" }
​
TM: Object { Lb: false, gx: false, Va: true, … }
​
hH: Object { Lb: false, gx: false, Va: true, … }
​
ids: "ga:176819049"
​
mP: Object { Lb: false, gx: false, Va: true, … }
​
zt: Object { iH: (9) […], gH: {…}, SM: {…}, … }

response2:回应2:

  []
​
length: 0
​
<prototype>: Array []

data from console.log(data["zt"])来自 console.log 的数据(数据 [“zt”])

​
zt: {
​​
SM: Object { "UA-xx2": {…}, "UA-xx-1": {…}, "UA-xx-1": {…}, … }
​​
gH: Object { 34199158: {…}, 44335927: {…}, 64056475: {…}, … }
​​
iH: Array(9) [ {…}, {…}, {…}, … ]
​​
r3: Object { 63179797: {…}, 77047380: {…}, 91559901: {…}, … }
​​
<prototype>: Object { fetch: fetch(), … }
​
<prototype>: Object { constructor: RE(a), execute: execute(), gt: gt(), … }
    }

as can be seen from the response above, iH is an array of length 9, but when I log it I get an empty array从上面的响应可以看出, iH是一个长度为 9 的数组,但是当我记录它时,我得到一个空数组

It might be an async problem, where the data object only gets filled after you console.log it.这可能是一个异步问题,其中数据 object 仅在您 console.log 后才被填充。 Try logging console.log({...data}) or console.log(JSON.stringify(data)) to make sure the data is there when f2 is called.尝试记录console.log({...data})console.log(JSON.stringify(data))以确保在调用 f2 时数据存在。

Background: When you console.log(data), and then look at it in the console, then it will contain changes that occur even after the console.log call.背景:当您使用console.log(data),然后在控制台中查看它时,它将包含即使在console.log 调用之后发生的更改。 This is because data is an object, and you are just logging the reference to the object, not the value of the object itself.这是因为数据是 object,而您只是记录对 object 的引用,而不是 object 本身的值。 Here's an example: https://jsfiddle.net/michaschwab/f61tdue5/3/这是一个例子: https://jsfiddle.net/michaschwab/f61tdue5/3/

try this way console.log(data[zt]) console.log(data[iH])试试这种方式console.log(data[zt]) console.log(data[iH])

or或者

console.log(data[zt],data[iH])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递给函数的对象中的Javascript数组为空 - Javascript array in object empty when passed to function 当我尝试向函数传递已传递数组的长度时,为什么会出现错误? - Why do I get error when I try to alert the length of the passed array to the function? 来自 API 调用的数据存储在一个数组中,但是当我尝试在 function 中使用该数组以供进一步使用时,它显示该数组为空。 为什么? - Data from API call are stored in a Array but when i try to use that array in a function for further use it shows that array is empty . why? fetch 给了我一个完整的数组,但是当我尝试访问它时它是空的 - fetch gives me a full array but it is empty when i try to access it 为什么第二个数组保持为空? - Why does the second array remain empty? 为什么我会丢失存储在数组中的数据,这是 object 的一个字段 - Why do I lose data stored in an array, which is a field of an object when I try to access it by 为什么当我尝试在 Next js 的数组中访问 object 时显示未定义 - Why It shows Undefined when I try Access object in array in Next js 当空数组通过ajax传递给函数时,为什么Array.Length = 1? - Why Array.Length = 1 when empty array is passed to function via ajax? 如何让我的默认属性数组访问插件中传递的jQuery对象? - How can I give my default array of properties access to the passed jQuery object in my plugin? 为什么当 function 数组不为空时我得到空响应? - Why I get an empty response when inside the function the array is not empty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM