Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

462
Views
Can't iterate over my array/object. Javascript, React Native

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. enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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?

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!