javascript中的数组出错

Trouble Making array in javascript

本文关键字:出错 数组 javascript      更新时间:2023-09-26

我在用javascript制作数组时遇到问题。首先我做了这样的

for(var i=1; i <= rowCount-1; i++)
    {  
        product[i-1] = [{
            'product_id' : $('#product_id' + i).val(),
            'name' : $('#item' + i).val(),
            'model' : $('#model' + i).val(),
            'reward' : $('#reward' +i).val(),
            'subtract' : $('#subtract' + i).val(),
            'minimum' : $('#minimum' + i).val(),
            'shipping' : $('#shipping' + i).val(),
            'tax_class_id' : $('#tax_class_id' + i).val(),
            'weight' : $('#weight' + i).val(),
            'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
            'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
            'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
        }];
    }

在ajax文章中将其作为参数传递之后,我可以看到它将其作为传递

product[0][0][minimum]  
product[0][0][model]    326
product[0][0][name] apple mac power
product[0][0][price]    100.0000
product[0][0][product_id]   50
product[0][0][quantity] 5
product[0][0][reward]   0
product[0][0][shipping] 1
product[0][0][subtract] 1
product[0][0][tax_class_i...    0
product[0][0][total]    500
product[0][0][weight]   0.00000000
product[1][0][minimum]  
product[1][0][model]    326
product[1][0][name] apple mac power
product[1][0][price]    100.0000
product[1][0][product_id]   50
product[1][0][quantity] 7
product[1][0][reward]   0
product[1][0][shipping] 1
product[1][0][subtract] 1
product[1][0][tax_class_i...    0
product[1][0][total]    700

但是我想要像这个一样的东西

product[0][name] = "apple mac power"

所以我把代码改成了这个

for(var i=1; i <= rowCount-1; i++)
{  
    product = [{
        'product_id' : $('#product_id' + i).val(),
        'name' : $('#item' + i).val(),
        'model' : $('#model' + i).val(),
        'reward' : $('#reward' +i).val(),
        'subtract' : $('#subtract' + i).val(),
        'minimum' : $('#minimum' + i).val(),
        'shipping' : $('#shipping' + i).val(),
        'tax_class_id' : $('#tax_class_id' + i).val(),
        'weight' : $('#weight' + i).val(),
        'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
        'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
        'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
    }];
}

所以在这样做之后,它只显示1行的数组,无论我有多少行计数,就像这个一样

product[0][minimum]  
product[0][model]    326
product[0][name] apple mac power
product[0][price]    100.0000
product[0][product_id]   50
product[0][quantity] 7
product[0][reward]   0
product[0][shipping] 1
product[0][subtract] 1
product[0][tax_class_i...    0
product[0][total]    700

有人能帮我吗。?

提前谢谢。。

您正在创建一个数组数组。取出第二个数组,如下所示:

for(var i=1; i <= rowCount-1; i++)
{  
    product[i-1] = {        // Removed array open
        'product_id' : $('#product_id' + i).val(),
        'name' : $('#item' + i).val(),
        'model' : $('#model' + i).val(),
        'reward' : $('#reward' +i).val(),
        'subtract' : $('#subtract' + i).val(),
        'minimum' : $('#minimum' + i).val(),
        'shipping' : $('#shipping' + i).val(),
        'tax_class_id' : $('#tax_class_id' + i).val(),
        'weight' : $('#weight' + i).val(),
        'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
        'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
        'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
    };       // Removed array close
}

在JavaScript中,[]创建一个数组,因此[{object}]创建一个单单元格数组。

还要记住,product[0]["name"]与product[0]相同。name——在JavaScript中,属性也可以用于索引语法。product是您的数组,数组中的每个单元格都是一个对象,其属性包括名称。

在您的第一个版本中,您将一个数组分配给数组,因此,它显示为2d数组。在第二个版本中,您总是将产品变量分配给新值,因此它只包含一个产品信息。所以你可以通过删除[和]像一样更新你的第一个版本

for(var i=1; i <= rowCount-1; i++)
{
    product[i-1] = {
        'product_id' : $('#product_id' + i).val(),
        'name' : $('#item' + i).val(),
        'model' : $('#model' + i).val(),
        'reward' : $('#reward' +i).val(),
        'subtract' : $('#subtract' + i).val(),
        'minimum' : $('#minimum' + i).val(),
        'shipping' : $('#shipping' + i).val(),
        'tax_class_id' : $('#tax_class_id' + i).val(),
        'weight' : $('#weight' + i).val(),
        'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
        'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
        'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
    };
}

或者您可以将第二个版本修改为

for(var i=1; i <= rowCount-1; i++)
{  
    product.push({
        'product_id' : $('#product_id' + i).val(),
        'name' : $('#item' + i).val(),
        'model' : $('#model' + i).val(),
        'reward' : $('#reward' +i).val(),
        'subtract' : $('#subtract' + i).val(),
        'minimum' : $('#minimum' + i).val(),
        'shipping' : $('#shipping' + i).val(),
        'tax_class_id' : $('#tax_class_id' + i).val(),
        'weight' : $('#weight' + i).val(),
        'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
        'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
        'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
    });
}

希望它会有所帮助。

product[0]['name'] = "apple mac power"

product[0].name = "apple mac power"

而不是

product[0][name] = "apple mac power"

你的第一种方法很好,但你添加了一个数组,里面有一个散列仅移除周围的[]

product[i-1] = {
        'product_id' : $('#product_id' + i).val(),
        'name' : $('#item' + i).val(),
        'model' : $('#model' + i).val(),
        'reward' : $('#reward' +i).val(),
        'subtract' : $('#subtract' + i).val(),
        'minimum' : $('#minimum' + i).val(),
        'shipping' : $('#shipping' + i).val(),
        'tax_class_id' : $('#tax_class_id' + i).val(),
        'weight' : $('#weight' + i).val(),
        'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
        'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
        'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
    };

而不是

product[i-1] = [{
        'product_id' : $('#product_id' + i).val(),
        'name' : $('#item' + i).val(),
        'model' : $('#model' + i).val(),
        'reward' : $('#reward' +i).val(),
        'subtract' : $('#subtract' + i).val(),
        'minimum' : $('#minimum' + i).val(),
        'shipping' : $('#shipping' + i).val(),
        'tax_class_id' : $('#tax_class_id' + i).val(),
        'weight' : $('#weight' + i).val(),
        'quantity' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').val(),
        'price' : $('table.order-table tbody tr[id='''+ i +''']').find('td.price').html(),
        'total' : $('table.order-table tbody tr td.quantity input[name=''quantity'+ i +''']').parent().parent().find('td.total').html()
    }];