2

So I have a few javascript arrays, ex. array1 = [1,2,3] and array2 = [4,5,6] and I want these arrays to represent the column data, not row data like it wants. How would I make the table to look like:

This

I've been pulling my hair out all day, and I can't seem to find a solution......is this even supported?

2 Answers 2

6

Going off aw04's good answer, here is a way to do it by creating a new array by transposing the columns and rows. Use this method if you are generating the table based on data you have in a javascript array.

var rows = [];

for (var i = 0; i < array1.length; i++) { 
    rows.push([array1[i],array2[i]]);
}

Just make sure that array1 has the same number of elements as array2.

Sign up to request clarification or add additional context in comments.

2 Comments

yep this is what i'd do actually if it will just create the table for you, my answer is probably an extra step
Yeah that's an easy way too, push them together. Thanks!
4

Simply output the data how you need it.

var rows = '';

for (var i = 0; i < array.length; i++) { //assumes equal size arrays
  rows += '<tr><td>'+array[i]+'</td><td>'+array2[i]+'</td></tr>';
  //or however you do this
}

5 Comments

He is using the datatables jquery plugin which accepts data as a javascript array. He just has to format his array differently.
yeah, that uses html right?, but I was looking to use datatables.net or bootstrap tables for its added functionality......so I guess I just have to figure out how to reformat the array?>
@Andrew In the most basic example it operates on an existing table, that's why i took this approach datatables.net/examples/basic_init/zero_configuration.html
Really, it doesn't matter though. You could use the same logic to restructure the data instead.
@aw04, that's a very clever approach. Maybe making that basic table and then converting it to the datatables.net might do the trick.....

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.