Q:

Javascript data into table

I have some HTML, JSON, and Javascript. I want to render a table consisting of 3 rows (as of JSON). Problem is that all my data comes out in TD's in one tr
<table>
<tbody>
    <tr>
        <td>car</td>
        <td>1000</td>
        <td>2000</td>
        <td>1500</td>
        <td>1000</td>
        <td>house</td>
        <td>500</td>
        <td>600</td>
        <td>700</td>
        <td>800</td>
        <td>internet</td>
        <td>100</td>
        <td>200</td>
        <td>2350</td>
        <td>1940</td>
    </tr>
</tbody>
</table>

I want
<table>
<tbody>
    <tr>
        <td>car</td>
        <td>1000</td>
        <td>2000</td>
        <td>1500</td>
        <td>1000</td>
    </tr>
    <tr>
        <td>house</td>
        <td>500</td>
        <td>600</td>
        <td>700</td>
        <td>800</td>
    </tr>
    <tr>
        <td>internet</td>
        <td>100</td>
        <td>200</td>
        <td>2350</td>
        <td>1940</td>
    </tr>
</tbody></table>

Here is my code
HTML
<div class="calc"></div>

JSON
{
"item": [
    {
        "name": "car",
        "cost": [1000, 2000, 1500, 1000]
    },
    {
        "name": "house",
        "cost": [500, 600, 700, 800]
    },
    {
        "name": "internet",
        "cost": [100, 200, 2350, 1940]
    }
]
}

Javascript 
$.getJSON("calc.json", function(data) {

    var $table = $('<table/>'),
        $row = $('<tr/>');

    for(var i = 0; i < data.item.length; i ++){
        $row.append( '<td>' + data.item[i].name + '</td>' );

        for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
            $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
        }
    }

    $('.calc').append($table.append($row));

});

EDIT a less code intensive version
$.getJSON("calc.json", function(data) {

    var $table = $("<table/>");

    $.each(data.item, function(k,v) {
        var $row = $("<tr/>");
        $row.append("<td>" + v.name + "</td>");

        $.each(v.cost, function(k,v) {
            $row.append("<td>" + v + "</td>");
        });

        $table.append($row);

    });

    $(".calc").append($table);

});

A:

Hi i have changed you function a little but now you can use below given function:-
$.getJSON("calc.json", function(data) {

        var $table = $('<table/>');

        for(var i = 0; i < data.item.length; i ++){
           var $row = $('<tr/>');
            $row.append( '<td>' + data.item[i].name + '</td>' );

            for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
                $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
            }
           $table.append($row);
        }

        $('.calc').append($table );

    });

A:

var $table = $('<table/>');

for(var i = 0; i < data.item.length; i ++){
    var  $row = $('<tr/>');
    $row.append( '<td>' + data.item[i].name + '</td>' );

    for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
        $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
    }
    $table.append($row)
}

$('.calc').append($table);

Demo

