如何在javascript中迭代键控数组? [重复]

时间:2020-12-05 21:21:43

This question already has an answer here:

这个问题在这里已有答案:

I need to group the rows out of a table that have a matching order number, and then iterate over the groupings.

我需要将行分组到具有匹配订单号的表中,然后迭代分组。

I have this code working which is creating the perfect array, data-wise:

我有这个代码工作,创建完美的数组,数据方式:

    var multItems = [];
    //combine items under orders,
    $('tr.order').each(function(){
        var orderNum = $(this).find('.ordernumber').val();
        if ( ($('tr.order .ordernumber[value="'+orderNum+'"]').length > 1 ) && !(orderNum in multItems) ){
            $('tr.order .ordernumber[value="'+orderNum+'"]').each(function(){
                if (!(orderNum in multItems)){
                    multItems[orderNum] = [];
                }
                multItems[orderNum].push(this);
            });
        }
    });
    //create new tr with order totals (of each item)
    for (var i = multItems.length - 1; i >= 0; i--) {
        //code
    };

But it creates an array with a length of 0, apparently, where multItems = [] but multItems[orderNumber] is defined.. just with no way to access it if I don't know the order numbers. I could make an array of the order numbers separately, but that feels like it must be the long way round. If I just create a numbered array, how do I know which number to pop the items from the orders into?

但它创建了一个长度为0的数组,显然,其中multItems = []但是multItems [orderNumber]被定义了......如果我不知道订单号,就无法访​​问它。我可以单独制作一个订单号的数组,但感觉它必须是很长的路。如果我只是创建一个带编号的数组,我如何知道将订单中的项目弹出哪个数字?

1 个解决方案

#1


2  

With your current code you have

使用您当前的代码

var orderNum = $(this).find('.ordernumber').val();

where val() returns a string and not a number. So when you are doing multItems[orderNum] it is a string.

其中val()返回一个字符串而不是一个数字。所以当你在做multItems [orderNum]时,它是一个字符串。

For the current code to work, you want to use a for in loop.

要使当前代码有效,您需要使用for循环。

for (var prop in multItems) {
    if( multItems.hasOwnProperty( prop ) ) {
        console.log(multItems[prop]);
    }
}

FYI: Order is not guaranteed. Also you should be using an object {} and not an array here.

仅供参考:订单无法保证。此外,您应该使用对象{}而不是数组。


Now the other thing you can do is to use parseInt to change the string into a number and than magically your for loop would start working. [This is assuming that ordernumber is a numeric value]

现在,您可以做的另一件事是使用parseInt将字符串更改为数字,而不是神奇的for循环将开始工作。 [这假设ordernumber是一个数值]

var orderNum = parseInt($(this).find('.ordernumber').val(), 10);

#1


2  

With your current code you have

使用您当前的代码

var orderNum = $(this).find('.ordernumber').val();

where val() returns a string and not a number. So when you are doing multItems[orderNum] it is a string.

其中val()返回一个字符串而不是一个数字。所以当你在做multItems [orderNum]时,它是一个字符串。

For the current code to work, you want to use a for in loop.

要使当前代码有效,您需要使用for循环。

for (var prop in multItems) {
    if( multItems.hasOwnProperty( prop ) ) {
        console.log(multItems[prop]);
    }
}

FYI: Order is not guaranteed. Also you should be using an object {} and not an array here.

仅供参考:订单无法保证。此外,您应该使用对象{}而不是数组。


Now the other thing you can do is to use parseInt to change the string into a number and than magically your for loop would start working. [This is assuming that ordernumber is a numeric value]

现在,您可以做的另一件事是使用parseInt将字符串更改为数字,而不是神奇的for循环将开始工作。 [这假设ordernumber是一个数值]

var orderNum = parseInt($(this).find('.ordernumber').val(), 10);