【问题标题】:jQuery Create variable name from each value in arrayjQuery从数组中的每个值创建变量名
【发布时间】:2013-05-20 12:09:54
【问题描述】:

取数组(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/

【问题讨论】:

  • 添加了一个小提琴...jsfiddle.net/sherman2k2/j3dWS
  • 您能否提供代码示例,您将如何处理这些数据?
  • 数据正在用于绘图。变量名用于引用时间和日期的数组值。所以基本上在图中,变量 ATMCXPSysTime 用于从该数组中绘制出点。目前,我有几个完全相同的代码,代码的唯一区别是引用表和更改变量名...
  • 尝试为每个表替换它。每个表的代码都变得有点长。 var ATMCXPDate = []; var ATMCXPTime = []; var ATMCXPName = 'HP Non-Stop End of Day - 第 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]; });
  • 你能把这段代码放在问题里吗?

标签: jquery arrays variables


【解决方案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

您的代码的一个明显错误是您在全局范围和函数中都创建了同名的变量。 即

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

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

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)

【讨论】:

  • 这当然可以很好地清理我的烂摊子。我认为从代码版本到下一个版本,我留下了一些代码,而不是注释掉其他代码。因此,您的代码也可以很好地提供单个数字。我试图实现的整体输出是每个具有数组值的唯一变量名。您的方法允许我返回单个值,但我需要数组中的值。这是我试图用循环替换的功能代码...jsfiddle.net/sherman2k2/ZBmMa
  • @user2401481 你可以循环访问 CriticalPath 并获得你需要的所有东西,尽管我会检查你更新的小提琴。
  • 感谢您投入的时间和精力。这对我有用。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多