1

My data seems to be an array of arrays I want to loop over it and use the data

If I debug:

console.log(alldata);

This is how the data looks

enter image description here

  1. Why does it say "Data" ?
  2. I cannot seem to get the data to display

This For Loop does not work

 for (var i = 0; i < alldata.length; i++) {
            console.log(alldata[0]);
 }

I would think that the length on that object would be "ok" , what am I doing wrong?

Also I have been trying a different type of loop with $.each and I'm not understanding the indexing

alldata, index, query ... I know that query will contain the contain but I wish to loop over them .

It does not seem that I can do query[index][0] and I don't know why.

Here is where I attempt to loop and add to a variable

$.each(alldata, function (index, query) {
            //console.log(alldata);
            //console.log(index);
            //console.log(query);
            //console.log(query[0][0]);
            //console.log(index);
            strData += "<tr>";
            strData += "<td>" + query[0][0] + "</td>"; //+ query.outage_k + "</td>";
            strData += "</tr>";

        });

Update:

I added in another column from the database table to get returned and what I currently see is

122461,4876192

As now I have 2 records

So with that is shows

enter image description here

 $.each(alldata.Data, function (index, query) {
    strData += "<td>" + query + "</td>"; 
 }

That is spitting out 122461,4876192 how do I use "query" for separation?

I want to end up doing this

<td>122461</td><td>4876192</td>
2
  • 4
    That's an object with an array of arrays in it. You want to iterate over alldata.Data. Commented Dec 15, 2015 at 4:32
  • You will also want to use 'i' not '0' e.g. console.log(alldata[i]); Commented Dec 15, 2015 at 4:34

3 Answers 3

1

It's not an array of arrays. It's an object containing an array.

Here's 4 different ways to iterate through your array. All of them are functionally equivalent. Use whichever one you like the most.

// native JavaScript, ES6+
alldata.Data.forEach(x => console.log(x));

// native JavaScript, ES5+
alldata.Data.forEach(function(x) {
  console.log(x);
});

// native JavaScript, all versions
for (var i=0; i<alldata.Data.length; i++) {
  console.log(alldata.Data[i]);
}

// jQuery
$.each(alldata.Data, function(i,x) {
  console.log(x);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Nice, ok, so this gives me the loop in which I see it spit out all those values, but now I want to be able to have more data so "x" would only be the first record, but what if i have a lot more data?
the function will be called once per element in the array. x is just a placeholder for the current iteration of the loop. The first time the loop runs, x will point to the first element (alldata.Data[0]). The second time the loop runs, x will point to the second element (alldata.Data[1]). And so on..
so for example this spits out "122461" as it should , but i'm confused on the function(x) as what about if the object containing a array with more data like in the image for example i have 0 : 122461 , but then if i had 1 : johnson 2: Denver etc... how would the loop work then to display it?
Post your actual data and the expected output in your question. Someone can help you better then.
1

When you log alldata it is returning

>Object {Data: Array[9]}

which means that your alldata variable is pointing to a object that has a property called Data that has an array of nine elements. To access the array you would do

alldata.Data 

1 Comment

I want to loop alldata.Data.forEach(function(x) { console.log(x); });
1

Your object has a property called "Data" so you'd have to refer to the array as alldata.Data and then loop through it that way.

EDIT: looks like @Austin is a faster typer!

1 Comment

I wish to loop over alldata.Data.forEach(function(x) { console.log(x); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.