无法遍历我的数组/对象.Javascript,本机反应 [英] Can't iterate over my array/object. Javascript, React Native

查看:25
本文介绍了无法遍历我的数组/对象.Javascript,本机反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白这里会发生什么.所以我有一个函数,它接受一个二维数组和一个字符串,并遍历二维数组并检查是否有任何子数组包含该字符串.但不知何故,我无法迭代这个对象/数组,我真的很困惑它到底是什么.我在 javascript 中做了很多迭代.我尝试过 for-in、for-of (es6)、C stlye(如下所示)、forEach(callback)、map... 没有任何效果.

I really don't understand what could be going on here. So I have a function which takes a 2d array and a string and iterates through the 2d array and checks if any subarrays contain the string. But somehow I can't iterate over this object/array and I'm really confused as to what it actually is. I've done lots of iteration in javascript. I've tried for-in, for-of (es6), C stlye(like below), forEach(callback), map... nothing works.

    _makeOrUpdateCase = (arrayOfArrays, str) => {
    console.log(arrayOfArrays); //returns the object/array shown in image below, expected
    console.log(typeof(arrayOfArrays)); //object
    console.log(Array.isArray(arrayOfArrays)); //true - huh? is this array or object??
    for (var i = 0; i < arrayOfArrays.length; i++) {
        console.log(arrayOfArrays[i]) //this does not work
        console.log(i); //nothing is printed out, as if the loop is simply ignored
    }

这是我得到的输出..你可以看到我在循环中打印的东西没有被执行.我知道 javascript 可能很奇怪,但是来吧这里发生了什么,我不知道要谷歌什么.我在这段代码中多次迭代数组和对象.

Here is the output I get.. you can see that the stuff I'm printing in the loop is not executed. I know javascript can be weird but c'mon what is going on here, I have no idea what to google. I've iterated over arrays and objects lots of times in this code.

推荐答案

tl;dr: 你的循环很好,但数组是空的,即使它出现在控制台中,它不是.

tl;dr: Your loop is fine but the array is empty even if it appears in the console that it is not.

您正在填充数组之前记录/访问它.从输出中的 Array[0](指示长度为 0 的数组)可以明显看出这一点,即使它似乎有一个元素.如果您有一个异步进程(如 Ajax),但在异步进程完成之前记录/访问数组,则可能会发生这种情况.

You are logging/accessing the array before it was populated. That's evident from the Array[0] in the output (indicating an array of length 0), even though it appears to have an element. This can happen if you have an asynchronous process, like Ajax, but are logging/accessing the array before the asynchronous process is done.

如果您将鼠标悬停在控制台中的小 i 上,它会告诉您该值是 just 求值的,即它显示该变量当时的值您单击三角形,而不是在您在代码中记录值时.当您点击三角形时,异步过程可能已经完成.

If you hover over the little i in the console, it will tell you that the value was just evaluated, i.e. it shows the value the variable has at the time you click the triangle, not at the time you log the value in code. By the time you click the triangle, the asynchronous process will likely have finished.

但是,如果您使用 console.log(JSON.stringify(arrayOfArrays)); 相反,您将看到数组为空.

However, if you use console.log(JSON.stringify(arrayOfArrays)); instead you will see that the array is empty.

以下是演示问题的简单示例(打开浏览器控制台并展开数组,至少适用于 Chrome):

Here is simple example that demonstrates the issue (open the browser console and expand the array, works in Chrome at least):

// Open the browser console
var arr = [];
console.log(arr);
arr.push(1,2,3);

唯一的解决方案是在异步过程完成后调用_makeOrUpdateCase.由于您没有显示调用函数的方式/时间/位置,因此对于该问题只能说这些.

The only solution is to call _makeOrUpdateCase only after the asynchronous process is done. Since you are not showing how/when/where the function is called, this is all that can be said about the problem.

相关:Chrome 的 JavaScript 控制台是否懒得计算数组?

这篇关于无法遍历我的数组/对象.Javascript,本机反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆