【问题标题】:Access dynamic object: javascript/jquery访问动态对象:javascript/jquery
【发布时间】:2012-07-05 13:43:33
【问题描述】:

我写了一些代码,在循环中调用了一个函数,它将循环迭代中的当前对象传递给函数。

问题是我不知道如何使用该对象,因为它是动态的,我只能访问 obj 参数。

Javascript

objectifyTableRow(val, i); // Populate object with properties and values

Val 是传递给函数 objectifyTableRow 的对象

function objectifyTableRow(objRow, count) { // Create objects for calculations

    var ii = 0;
    $('#ctl00_PageContent_freight_rate_column_chaair_r' + count + " " + 'td').each(function(i, val) { // begin each
        /* Concatenate column name with subcolumn name. example objFobCost.China/Sea Origin,Air */
        if (i < 3) { // columns 0,1,2 in table row
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[0] + arraySubColumn[ii];


                objRow[PropertyName] = parseFloat($(val).html()); // Set key name with var PropertyName 

                ii += 1;
            }

            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if

        else if (i > 2 & i < 6) {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[1] + arraySubColumn[ii];

                objRow[PropertyName] = parseFloat($(val).html());

                ii += 1;
            }

            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if

        else if (i > 5 & i < 9) {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[2] + arraySubColumn[ii];

                objRow[PropertyName] = parseFloat($(val).html());

                ii += 1;
            }

            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if

        else if (i > 8 & i < 12) {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[3] + arraySubColumn[ii];

                ii += 1;
            }

            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if

        else {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[4] + arraySubColumn[ii];

                           ii += 1;
            }

            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of else
    });                  // end of each loop TD
    beginCalc(objRow);
};

每个对象都传递给 beginCalc,因此可以根据 ID、Key 和 value 进行计算

function beginCalc(obj) {
    // Every obj used is passed to here
    $.each(obj, function(key, element) {
    alert('ID: ' + obj[this.id] + '\n' + 'key: ' + key + '\n' + 'value: ' + element); // Check correct obj id, key and value
});

我这样做的原因是因为对象存储来自 asp.net 网格的值,我认为为每个网格行创建对象然后通过选择 obj.key 在代码中进行计算会更简洁:值 * obj.key:值。而不是使用 getDocumentByElementId。

任何想法如何通过 beginCalc 函数中的 ID 以编程方式访问这些对象?

谢谢

【问题讨论】:

  • “对象参数”是什么意思?你有对象的键和值,你在说什么参数?
  • 当我调用 beginCalc() 时,我提供一个对象作为它的参数。由于此函数调用发生在循环中,因此我将传递每个对象,直到循环结束。所以在 beginCalc() 我有多个具有不同 ID 的对象。每个 obj 也有自己的键值对。这更有意义吗?
  • 我明白,但您在寻找什么?您希望您传递的所有对象的数组在beginCalc() 中可用?在这种情况下,您需要先创建完整的数组,将其保存为变量,然后对其进行迭代……没有办法神奇地获取尚未创建的对象列表。也许我还是误会了。
  • 我想知道如何通过 ID 以编程方式访问对象。所有对象的数组可能会有所帮助。
  • 如果它们都有特定的 ID,您希望最终得到一个将这些 ID 映射到其对应对象的对象。所以是的,遍历你原来的“对象对象”并构建一个新的对象,将 ID 分配为每个对象的键。也许像underscore.js 这样的库可以帮助您保持某些数据结构的井井有条。

标签: javascript jquery object loops


【解决方案1】:

您想要的是 DOM 节点和值之间的连接。试试这个方法(伪代码):

var nodes = $(...select all the nodes...);
nodes.each( function( index, node ) {
    var obj = { node: node };
    beginCalc(obj);
} );

beginCalc() 中,您现在可以使用obj.node 访问DOM 节点。您可以使用相同的方法将更多数据放入对象中,以将所有内容保存在一个位置(您需要更新的 DOM 节点 + 您需要更新它的所有数据)。

【讨论】:

  • 当您说“$(...select all the nodes...);”时,我不确定您的意思是什么?
  • 我的意思是$('#ctl00_PageContent_freight_rate_column_chaair_r' + count + " " + 'td')。重点是应该为每个节点创建一个obj,而不是将PropertyName下的所有节点值保存在一个对象中。
猜你喜欢
  • 2013-05-05
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 2011-08-10
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多