bjskistad bjskistad - 7 months ago 18
HTML Question

Array Data in Table JS

I want to take the contents of two arrays

collected = [1, 0, 2,]
and
teacher = ['Zimmerman', 'Tischler', 'Johnson']
and display those arrays in a table by using the
sort()
method. So that is would look like this:

Johnson - 2

Zimmerman - 1

Tischler - 0

Would anyone know how to do this?

Answer

Complete example:

<table id="tab" border="1">

</table>
<script type="text/javascript">

    var
        collected,
        teacher,
        table,
        data = '',
        source = [];

    collected = [1, 0, 2,];
    teacher = ['Zimmerman', 'Tischler', 'Johnson'];

    for (var i in collected) {

        source.push([collected[i], teacher[i]]);
    }

    source.sort();

    table = document.getElementById('tab');

    for (var i in source) {

        data += '<tr><td>' + source[i][0] + '</td><td>' + source[i][1] + '</td></tr>';
    }

    table.innerHTML = data;

</script>

Result:

enter image description here

Comments