Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/07/29

Solution to the Javascript Object problem

eval()

I should've seen it a while ago.

  1. var jsonA = new Array() ;  
  2. var sample_array = [ "foo" , "bar" , "blee" ] ;  
  3. for ( var i = 100 ; i <= 290 ; i++ ) {  
  4.     for ( j in sample_array ) {  
  5.         var k = i * i * (j+1) ;  
  6.         k = '"' + k.toString() + '"' ;  
  7.         jsonA.push( sample_array[j] + i + " : " + k ) ;  
  8.         }  
  9.     }  
  10.     var json = jsonA.join(" , ") ;  
  11.     x = eval( "( { " + json + " } )" ) ;  
That is, create an array. put each name:value pair into the array in the name colon value format. Join 'em with commas. Wrap with curly braces ("This is an Object, JS") and parens ("I'm JS. I'm a language. It's my job to have crazy syntax.") Run eval(). I don't show my JSON step, but it's basically taking the params and pushing them back. The interesting thing is, with the data I made up here, the top is between 570 and 585 elements. I don't know if that's an HTTP limitation, an eval() limitation or a jQuery limitation, but I know that once I hit that, the code flakes. Which is interesting.

No comments:

Post a Comment