jQuery Create variable name from each value in array -


take array (critpath) , each item in array; check table , split columns separate arrays (sysdate & systime).

return (critpath) name plus (systime) name form new variable name has systime array value (ie. atmcxpsystime = [2,2]). each in critpath array.

so far, i've been able output array values have been unsuccessful in creating variable names (critpath) value of (sysdate) and/or (systime).

ultimately i'd able call atmcxpsystime variable name , have give value [2,2] , same other in (critpath)... ccsystime output [6,5]

the scripting...

var critpath = [] critpath = ['atmcxp','cc'] var sysdate = []; var systime = [];  $(function(sys){     (i in critpath) {         var sys = critpath[i];         var sysdate = sys+'date';         var systime = sys+'time';          (i in sys) {             $('#' + critpath[i] + ' tbody tr').each(function(index) {                 var $this = $(this);                 var str = $(this).find(":nth-child(2)").html()                 var parts = str.split(":");                 var minutes = parseint(parts[0], 10) * 60 + parseint(parts[1], 10);                  sysdate[index] = [$(this).find(":nth-child(1)").html()];                 systime[index] = [index] = [minutes];             });         }     }     return sys; });  alert(systime); alert(critpath); alert(sys); 

simple html tables...

<table border="1" id="atmcxp" cellspacing="1" align="center">     <tbody>         <tr>             <td>2013-04-09</td>             <td>00:02</td>         </tr>         <tr>             <td>2013-04-10</td>             <td>00:02</td>         </tr>     </tbody> </table> <table border="1" id="cc" cellspacing="1" align="center">     <tbody>         <tr>             <td>2012-04-09</td>             <td>00:06</td>         </tr>         <tr>             <td>2012-04-10</td>             <td>00:05</td>         </tr>     </tbody> </table> 

edit: added fiddle... http://jsfiddle.net/sherman2k2/j3dws/

update on updated fiddle

it seems got trying , problem is. there 2 lines of code in updated fiddle

    atmcxpdate[index] = [$(this).find(":nth-child(1)").html()]; //set dates found in column array , assign variable name      atmcxptime[index] = [minutes]; //set converted minutes column array , assign variable name 

which either typo or forget declare index variable. want have 1 array dates , times tables have , replace with

   atmcxpdate.push($(this).find(":nth-child(1)").html());    atmcxptime.push(minutes);  

we doing 2 things here, putting parsed date , time arrays , putting plain values , not wrapped arrays had before, don't need array inside array.

update on performance of original code:
there several things seems logical errors

  • you have cycle for ( in sys)
  • you can select table once , iterate through rows after ( $('#' + critpath[i] + ' tbody tr') replaced $('#'+critpath[i]).find('tr').each

one obvious error code creating variables same name both in global scope , in function. ie

... // here created global variables , assigned them empty arrays var sysdate = []; var systime = [];  $(function(sys){     (i in critpath) {          // here created local variables same names global ones no more accessible within function         var sysdate = sys+'date';         var systime = sys+'time';      (i in sys) {         $('#' + critpath[i] + ' tbody tr').each(function(index) {             ...             // here assigned local variables ,             sysdate[index] = [$(this).find(":nth-child(1)").html()];              // line doesnt make sense             systime[index] = [index] = [minutes];             // meant              systime[index] = [minutes];         });     }    } // next line doesn't make sense not used anywhere return sys; }); // here alerting global variables, wasn't affected ondomready    handler alert(systime); alert(critpath); alert(sys); 

overall if want create variable in global scope particular name can using window object

function setvar () {   var nameofglobalvariable = 'systime0202'   window[nameofglobalvariable] = 'some value' }  setvar()  console.log(systime0202)  // outputs 'some value' 

i suspect don't need global variables different names , instead need pair of array both dates , times have. expect want lookup 1 date / time , corresponding value array , different critical path set of critical paths.

you can try

var criticalpathdata = [] $(function(){     // looping through named tables grab date/times   (var in critpath) {      var cp = critpath[i]       , table = $('#' + cp)       // object hold date/times particular critical path       , cpdatetime = { items : [] }           table.find('tr').each(function(index, row) {             var $this = $(this)               , tds = $this.find('td')               , rowdate = tds[0].innerhtml               , rowtime = tds[1].innerhtml               , parsedtime = rowtime.split(':')             cpdatetime.items.push({ date: rowdate , time : +parsedtime[0] * 60 + +parsedtime[1] })           });       // put parsed dates/times items critical path global object       criticalpathdata.push(cpdatetime)    }     // after filled in data can access here    alert(criticalpathdata[0].items[0].date)    alert(criticalpathdata[0].items[0].time) })     // or here alert(criticalpathdata[0].items[0].date) alert(criticalpathdata[0].items[0].time) 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -