Another thought is that you could just do a replace on 0, 1 and 2 in the string: replace each number with the div you want, then wrap that in a div.

- Jack

Jack Killpatrick wrote:
assuming that your json items are objects, try this. The console statements are for Firebug output: comment them out if you don't have firebug. This uses a few speed tricks.

<script language="javascript">
   // sample data
   //
   var items = {};
items["1"] = '101010111110010101020110111110100010101020101020101010101100110100'; items["2"] = '000010101210101011100101101010000111111001010121010000111110001111'; var html = []; // we pop html into here and then join it to get the html snippet for our page
   for(var item in items){
       html.push(process(items[item]));
   }
console.log(html.join('')); // your final result comes from html.join('');
     function process(item){
       var wrapper = [];
wrapper.push('<div class="wrapper">'); // I used a class here to make it easier to see in results var ar = item.split(''); // splits the string with all the numbers into an array
       for(var i=0;i<ar.length;i++){
console.log(ar[i]); // outputs each individual item in the string to the console

           switch (ar[i]){
               case '0':
                   wrapper.push('<div class="grey">&nbsp;</div>');
                   break;
               case '1':
                   wrapper.push('<div class="blue">&nbsp;</div>');
                   break;
               case '2':
                   wrapper.push('<div class="red">&nbsp;</div>');
                   break;
           }
       }
       wrapper.push('</div>');
return wrapper.join(''); // returns the wrapper with your divs in it
   }
</script>

- Jack

René wrote:
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