That "for( i in items )" loop isn't guaranteed to enumerate the items in any
particular order. If you need something to be in a particular order, don't
use an object with string property names. Use an array, e.g.

    var items = [
 
"101010111110010101020110111110100010101020101020101010101100110100",
 
"000010101210101011100101101010000111111001010121010000111110001111",
        ...
    ];

Then you can code:

    var classes = [ 'grey', 'blue', 'red' ];
    var html = [], h = -1;
    html[++h] = '<div>';
    for( var item, i = -1;  item = items[++i]; ) {
        html[++h] = '<div class="row">';
        var values = item.split('');
        for( var value, j = -1;  value = values[++j]; ) {
            html[++h] = '<div class="';
            html[++h] = classes[+value];
            html[++h] = '">&nbsp;</div>';
        }
        html[++h] = '</div>';
    }
    html[++h] = '</div>';
    html = html.join('');

I added a wrapper div around each row with class="row" - my guess is you
probably need something like that, because you want this to be a
two-dimensional display, right?

-Mike

> From: René
> 
> I have some JSON that needs processing, e.g.:
> 
>    items["1"] =
> '101010111110010101020110111110100010101020101020101010101100110100";
>    items["2"] =
> '000010101210101011100101101010000111111001010121010000111110001111";
>    ... (x 1000)
> 
> I need to process ~1000 rows so that each 0, 1 or 2 appear as 
> a small coloured dot. (It's a visualization thing).
> So here's what I have so far, which works:
> 
> 
> 
> for (i in items) {
>    html += process (items[i]);
>    }
> 
> function process (item) {
>       var result = '<div>';
>       for (var g=0; g<item.length; g++) {
>               switch (item.substr(g,1)) {
>                       case "0":
>                               result += '<div 
> class="grey">&nbsp;</div>';
>                               result;
>                       case "1":
>                               result += '<div 
> class="blue">&nbsp;</div>';
>                               break;
>                       case "2":
>                               result += '<div 
> class="red">&nbsp;</div>';
>                               break;
>                       }
>               }
>       result += '</div>';
>       return result;
>       }
> 
> 
> My question is, is there a faster or more efficient way to 
> iterate through each items' "10101001010220211"? I realize 
> this is not strictly jQuery related, but it seems the 
> smartest Javascript people hang out here. :-)
> 
> Thanks
> 
> ...Rene
> 

Reply via email to