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

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

问题描述

我真的不明白这里会发生什么。所以我有一个函数,它接受一个二维数组和一个字符串,并遍历2d数组并检查是否有任何子数组包含该字符串。但不知怎的,我不能迭代这个对象/数组,我真的很困惑它实际上是什么。我在javascript中做了很多次迭代。我试过for-in,for-of(es6),C stlye(如下),forEach(回调),map ...没什么用。

  _makeOrUpdateCase =(arrayOfArrays,str)=> {
console.log(arrayOfArrays); //返回下面图片中显示的对象/数组,预期
console.log(typeof(arrayOfArrays)); // object
console.log(Array.isArray(arrayOfArrays)); //真 - 是吗?是这个数组还是对象?
for(var i = 0; i< arrayOfArrays.length; i ++){
console.log(arrayOfArrays [i])//这不起作用
console.log(i) ; //没有打印出任何内容,好像简单地忽略了循环
}

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

解决方案

tl; dr:您的循环没问题但数组是空的,即使它出现在控制台中也不是。



您正在之前记录/访问数组人口稠密。从输出中的 Array [0] 中可以看出这一点(表示长度为0的数组),即使它出现也有一个元素。如果您有异步进程(如Ajax),但在异步进程完成之前正在记录/访问数组,则会发生这种情况。



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



但是,如果您使用 console.log(JSON.stringify( arrayOfArrays)); 而不是你会看到数组是空的。



这是演示问题的简单示例(打开浏览器控制台和扩展数组,至少在Chrome中运行):



  //打开browser consolevar arr = []; console.log(arr); arr.push(1,2,3);  



唯一的解决方案是在异步过程完成后仅调用 _makeOrUpdateCase 。由于您没有显示调用函数的方式/时间/位置,因此可以说明问题所在。






<相关: Chrome的JavaScript控制台是否懒于评估数组?


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
    }

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: Your loop is fine but the array is empty even if it appears in the console that it is not.

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.

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.

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

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);

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.


Related: Is Chrome's JavaScript console lazy about evaluating arrays?

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

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