0

I am using ReactJS. I have a JSON:

{
    "randomNum1": [
        1,
        2,
        3,
        4
    ],
    "randomNum2": [
        5,
        6,
        7,
        8
    ]
}

What I wanted to do is to get the array of randomNum1 and randomNum2. This is what I did:

for(let i = 0; i<data.randomNum1.length; i++)

and I get this error: Cannot read property 'length' of undefined

The reason why I did that is because when I do a console.log(data.randomNum1) I am able to see the array: [array][1]

Is it because it's still an Object which is why .length is not allowed? If so, how can I get the values of those numbers and store it in an array? [1]: https://i.stack.imgur.com/nLbdA.png

user123
  • 5
  • 2
  • Does this answer your question? [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Nick Parsons Nov 01 '20 at 14:40
  • The loop you show should work on the object you show. Please provide a runnable [mcve] that shows the problem. Thanks. – ggorlen Nov 01 '20 at 14:40
  • try doing `data['randomNum1'].length` – Ankush Verma Nov 01 '20 at 14:46
  • @NickParsons if I were to stringify my json, how can I convert it to array? I can't slice cause I am not sure the length of the random numbers – user123 Nov 01 '20 at 15:11
  • @user123 the solution isn't to stringify your object. What I'm guessing is happening is that your object is being populated asynchronously, meaning at the time that you do your `console.log()` the object's keys aren't set yet. Since chrome will log a live reference to the object, you're not necessarily seeing its state at the time that it is logged. The answer in the link suggests that you can check if this is the case by doing `console.log(JSON.stringify(data))` to see what the state of your object really is at the time of logging it. – Nick Parsons Nov 01 '20 at 15:17
  • so how can I make it such that when I try to get the key, I will have the data? sorry am new in reactjs, and isn't very good with asynchronous – user123 Nov 01 '20 at 15:45
  • @user123 we'd need more details such as how your object is being populated with data. You should confirm what you see when you do `console.log(JSON.stringify(data))` – Nick Parsons Nov 02 '20 at 01:53

2 Answers2

0
let allValues=[]
let the_json_object={
    "randomNum1": [
        1,
        2,
        3,
        4
    ],
    "randomNum2": [
        5,
        6,
        7,
        8
    ]
}
const keys = Object.keys(the_json_object);
const values = Object.values(the_json_object);
let Key_len = keys.length;
for(i=0;i<Key_len;i++){
      let len = values[i].length;
      for(j=0;j<len;j++)
        { 
            if(values[i][j])
               {
                  allValues.push(values[i][j])
                }
         }
 }
//allValues contains all the values from all arrays.
Ankush Verma
  • 689
  • 1
  • 8
  • 17
0

Your data object is probably undefined at the moment you are entering the loop. The reason you see it on console.log is because objects are passed by reference. When you console.log the object and then click to see his properties you get the updated object. If you'll do console.log(JSON.stringify(data)) you will see the object is probably empty.

Please provide more code in order to understand the issue here.

nir shabi
  • 367
  • 1
  • 15
  • I only have the data and I am trying to get the array values out of the Object. I have no code because I have no idea how to do that since what I did is incorrect – user123 Nov 01 '20 at 15:48