1

I really suck at javascript, but you have to learn. I'm trying to loop thru a json string to build a table. It's working (kind of). But one piece is not working. I try to loop thru an array of booleans. If it's true, add a column with the text "yes", if it's false add one with "no". But that part won't work. It won't add any values at all!

Additional suggestions of my code is highly appreciated:

var jsonstr = '{"no_of_places": 4, "teams": {"Player 1": {"done": [true, true, true, false], "time": null},    "Player 2": {"done": [true, true, true, true], "time": "0 dagar 1:10:21"}, "Player 3": {"done": [true, true, true, true], "time": "0 dagar 2:47:34"}}}';

$(document).ready(function () {
    var result = jQuery.parseJSON(jsonstr);
    var theadTr = $('.scorestable thead tr');

    theadTr.append('<th>Team</th>');
    // Adds one header for each place
    for (var i = 0; i < result.no_of_places; i++) {
        theadTr.append('<th>' + (i + 1) + '</th>');
    }

    // Add teams and their result.
    $.each(result.teams, function (index, value) {
        var row = ['<tr><td>', index, '</td><td>'];
        // Add if place is found or not.
        $(this.done).each(function () {
            if (this === true) {
                row.concat(['<td>yes</td>']);
            } else {
                row.concat(['<td>no</td>']);
            }
        });

        $('.scorestable tbody').append(row.join('') + '</tr>');
    });
});

Simple HTML template:

<p></p>
<table class="scorestable">
    <thead>
        <tr></tr>
    </thead>
    <tbody></tbody>
</table>

Hints (or note to self, if you want)

  1. I really learned alot from this simple snippet from Kevin B:

    $.each(["foo","bar","foobar"],function(i,val){
       console.log(typeof this,typeof i,typeof val); 
     });
    
     // OUTPUTS:
     // ========
     // object number string
     // object number string
     // object number string
    
  2. Arrays are immutable (edit if I use wrong term) in javascript.

    // So instead of:
    origArray.concat(['more', 'values']);
    
    // I need to write:
    origArray = origArray.concat(['more', 'values']);
    

5 Answers 5

2

You have added extra td JSFIDDLE

$.each(result.teams, function (index, value) {
        var row = ['<tr><td>', index, '</td>']; // extra td
        // Add if place is found or not.
        $(this.done).each(function () {
            if (this === true) {
                row = row.concat(['<td>yes</td>']);
            } else {
                row = row.concat(['<td>no</td>']);
            }
        });

        $('.scorestable tbody').append(row.join('') + '</tr>');
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Oh. Stupid of me. That was one problem, but I can't get it to work anyway: jsfiddle.net/cx3VA/8
@NiclasNilsson I made required changes in jsfiddle check link
Oh, It did'nt quite work. But your code made me see my misstake. I thought row where a mutable object and I didn't need to put row = row.concat... I could figure it out myself from your fiddle but it was'nt quite what I was asking for. So an upvote! I wish I could give you two :-p Lars-August nailed what I already figured out though.
1

Here you go. The each() method needs an index and value. The value is your boolean.

 // Add teams and their result.
$.each(result.teams, function (index, value) {
        var row = ['<tr><td>', index, '</td>']; // extra td
        // Add if place is found or not.
        $(this.done).each(function (index, value) {
            if (value === true) {
                row = row.concat(['<td>yes</td>']);
            } else {
                row = row.concat(['<td>no</td>']);
            }
        });

        $('.scorestable tbody').append(row.join('') + '</tr>');

    });

Tested and working

Comments

1

You need change === on ==, because this is boolean object and this === true always.

Try this code:

if (this == true) {
  row.concat(['<td>yes</td>']);
} else {
   row.concat(['<td>no</td>']);
}

1 Comment

this in that case should be a boolean, he's looping over an array of booleans.
1

I'd suggest not using this inside of $.each, it isn't exactly what you would expect.

http://jsfiddle.net/EzTL7/

Instead, use the second parameter that you named value

 $.each(result.teams, function (index, value) {
    var row = ['<tr><td>', index, '</td>'];
    // Add if place is found or not.
    $(value.done).each(function () {
        if (this === true) { // `this` is ok here because you're using $.fn.each and not $.each
            row.concat(['<td>yes</td>']);
        } else {
            row.concat(['<td>no</td>']);
        }
    });

    $('.scorestable tbody').append(row.join('') + '</tr>');
});

Comments

1

You should use $.each for iterating the array.

   $.each(this.done, function (i, v) {
        if (v === true) {
            row = row.concat(['<td>yes</td>']);
        } else {
            row = row.concat(['<td>no</td>']);
        }
        console.log(row);
    });

And you use concat in the wrong way. concat won't change callee's value, instead you should use the return value:

row = row.concat(['<td>yes</td>']);

A working example for your code.

Comments

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.