Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Take array (CritPath) and for each item in array; check table and split the columns into separate arrays (SysDate & SysTime).

Return the (CritPath) name plus (SysTime) name to form new variable name that has the SysTime array value (ie. ATMCXPSysTime = [2,2]). Do this for each in CritPath array.

So far, I've only been able to output the array values but have been unsuccessful in creating variable names from (CritPath) with the value of (SysDate) and/or (SysTime).

Ultimately I'd like to be able to call ATMCXPSysTime variable name and have it give the value [2,2] and the same for the other in (CritPath)... CCSysTime would output [6,5]

The scripting...

var CritPath = []
CritPath = ['ATMCXP','CC']
var SysDate = [];
var SysTime = [];

$(function(Sys){
    for (i in CritPath) {
        var Sys = CritPath[i];
        var SysDate = Sys+'Date';
        var SysTime = Sys+'Time';

        for (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/

share|improve this question
create fiddle please – rajesh kakawat May 20 at 12:13
Added a fiddle... jsfiddle.net/sherman2k2/j3dWS – user2401481 May 20 at 12:58
Can you provide example of code, what you going to do further on with this data? – vittore May 20 at 13:06
The data is being used in graphing. The variable name is used to reference the array value of time and date. So basically in the graphs, the variable ATMCXPSysTime is used to graph out points from the that array. Currently, I have several of the same exact code with the only difference in the code is referencing the table and changing the variable names... – user2401481 May 20 at 13:15
Trying to replace this for each table. This is getting a little long in code for every table. var ATMCXPDate = []; var ATMCXPTime = []; var ATMCXPName = 'HP Non-Stop End of Day - Part 1 - ATMCXP' $('#ATMCXP 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); ATMCXPDate[index] = [$(this).find(":nth-child(1)").html()]; ATMCXPTime[index] = [minutes]; }); – user2401481 May 20 at 13:17
show 1 more commentadd comment (requires an account with 50 reputation)

2 Answers

up vote 1 down vote accepted

Update on updated fiddle

It seems I got what you are trying to do and what you problem is. There is two lines of code in your updated fiddle

    ATMCXPDate[index] = [$(this).find(":nth-child(1)").html()]; //set dates found in column into array and assign variable name

    ATMCXPTime[index] = [minutes]; //set converted minutes from column into array and assign variable name

which is either typo or you forget to declare index variable. As you want to have one array for all dates and another for all times from all tables that you have , you are to replace it with

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

We are doing 2 things here, putting just parsed date and time into arrays and putting just plain values , not wrapped up into arrays as you had it before, you don't need array inside array.

Update on performance of original code:
There are several things that seems to be logical errors

  • You have extra cycle for ( i in Sys)
  • You can select table once and iterate through rows after that ( $('#' + CritPath[i] + ' tbody tr') replaced with $('#'+CritPath[i]).find('tr').each

One obvious error with your code is that you are creating variables with the same name both in global scope and in the function. ie

...
// here you created global variables and assigned them to empty arrays
var SysDate = [];
var SysTime = [];

$(function(Sys){
    for (i in CritPath) {

        // here you created local variables with the same names so global ones are no more accessible within this function
        var SysDate = Sys+'Date';
        var SysTime = Sys+'Time';

    for (i in Sys) {
        $('#' + CritPath[i] + ' tbody tr').each(function(index) {
            ...
            // here you assigned local variables , but
            SysDate[index] = [$(this).find(":nth-child(1)").html()];

            // this line doesnt make sense
            SysTime[index] = [index] = [minutes];
            // you probably meant 
            SysTime[index] = [minutes];
        });
    }
   }
// next line just doesn't make any sense as it is not used anywhere
return Sys;
});
// here you alerting global variables, that wasn't affected by your ondomready    handler
alert(SysTime);
alert(CritPath);
alert(Sys);

Overall if you want to create variable in the global scope with particular name you can do this using window object

function setVar () {
  var nameOfGlobalVariable = 'SysTime0202'
  window[nameOfGlobalVariable] = 'some value'
}

setVar()

console.log(SysTime0202)  // outputs 'some value'

I suspect that you really don't need global variables with different names and instead you need just pair of array for both dates and times you have. I expect that you want to do a lookup by one date / time and get corresponding value from another array , just different critical path from you set of critical paths.

You can try do to something like

var CriticalPathData = []
$(function(){ 

  // looping through named tables to grab all date/times
  for (var i in CritPath) {

    var cp = CritPath[i]
      , table = $('#' + cp)
      // this is object which will hold all date/times for 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 for all items with critical path to global object
      CriticalPathData.push(cpDateTime)
   }

   // after you filled in all the data you can access it 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)
share|improve this answer
This certainly cleans up my mess a good bit. I think going from version of the code to the next, I was leaving pieces of code in and not commenting out others. So your code works well for giving individual numbers as well. The overall output that I'm trying to achieve is a unique variable name for each that has a value of an array. Your method allows me to return single values but I need those within an array. This is my functional code that I'm trying to replace with loops... jsfiddle.net/sherman2k2/ZBmMa – user2401481 May 20 at 14:16
@user2401481 you can loop through CriticalPath and get all the stuff you need, although I'll check your updated fiddle. – vittore May 20 at 15:48
@user2401481 check updated answer. – vittore May 20 at 15:55
Thanks for the time and effort you've put in. This has worked for me. Many thanks! – user2401481 May 21 at 14:30
add comment (requires an account with 50 reputation)

You could do something like:

var CritPath = []
CritPath = ['ATMCXP','CC']
var SysDate = [];
var SysTime = [];

$(function(Sys){
    for (i in CritPath) {
        var Sys = CritPath[i];
        var SysDate = Sys+'Date';
        var SysTime = Sys+'Time';

        for (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);

                window[SysDate] = [$(this).find(":nth-child(1)").html()];
                window[SysTime] = [index] = [minutes];
            });
        }
    }
    return Sys;
});

alert(window['ATMCXPTime']);
alert(window['ATMCXPDate']);
alert(window['CCTime']);
alert(window['ATMCXPDate']);
share|improve this answer
I tried this but it broke the SysDate & SysTime arrays. The alerts only showed the last two cells from the "CC" table. – user2401481 May 20 at 12:57
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.