2

取数组(CritPath)和数组中的每一项;检查表并将列拆分为单独的数组(SysDate 和 SysTime)。

返回 (CritPath) 名称加上 (SysTime) 名称以形成具有 SysTime 数组值的新变量名称(即 ATMCXPSysTime = [2,2])。对 CritPath 数组中的每个执行此操作。

到目前为止,我只能输出数组值,但未能成功地从 (CritPath) 创建具有 (SysDate) 和/或 (SysTime) 值的变量名。

最终,我希望能够调用 ATMCXPSysTime 变量名称并让它给出值 [2,2] 并且在(CritPath)中的另一个值相同...... CCSysTime 将输出 [6,5]

脚本...

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

简单的html表格...

<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>

编辑:添加小提琴... http://jsfiddle.net/sherman2k2/j3dWS/

4

1 回答 1

1

更新更新的小提琴

看来我得到了你想要做的事情和你的问题。您更新的小提琴中有两行代码

    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

这要么是错字,要么你忘记声明index变量。由于您希望所有日期都有一个数组,而您拥有的所有表中的所有时间都有另一个数组,因此您将其替换为

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

我们在这里做两件事,将刚刚解析的日期和时间放入数组中,并只放入普通值,而不是像以前那样包装到数组中,你不需要数组内的数组。

原始代码性能更新:
有几件事似乎是逻辑错误

  • 你有额外的周期 for ( i in Sys)
  • 您可以选择一次表并在之后遍历行($('#' + CritPath[i] + ' tbody tr')替换为$('#'+CritPath[i]).find('tr').each

您的代码的一个明显错误是您在全局范围和函数中创建具有相同名称的变量。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);

总体而言,如果您想在全局范围内创建具有特定名称的变量,您可以使用 window 对象执行此操作

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

setVar()

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

我怀疑您确实不需要具有不同名称的全局变量,而您只需要一对数组来表示您拥有的日期和时间。我希望您想按一个日期/时间进行查找并从另一个数组中获取相应的值,只是与您的一组关键路径不同的关键路径。

你可以尝试做类似的事情

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)
于 2013-05-20T13:38:20.223 回答