简体   繁体   English

使用inArray()检查javascript数组中的数字

[英]Checking for numbers in a javascript array using inArray()

I am building a web cart for a list of products by adding them to an array called 'oitems'. 我正在通过将产品添加到名为“ oitems”的数组中来构建产品列表的网络购物车。 I'm using JSON syntax. 我正在使用JSON语法。 The following function is designed to accept a 'quant' (product quantity), 'prod' (product id), 'name' (product name), 'price' (product price) and 'order' (an object containing the 'oitems' array). 以下函数旨在接受“数量”(产品数量),“产品”(产品ID),“名称”(产品名称),“价格”(产品价格)和“订单”(包含“商品”的对象'数组)。 The funtion fires when someone clicks the 'add' button next to a product. 当有人单击产品旁边的“添加”按钮时,该功能将触发。

The 'prod' (product ids) range from 1-30. “产品”(产品ID)的范围是1-30。 I want the function to add to the 'oitems' array when a new product is added and update existing quantities of a product if someone decides they want to increase the 'quant' of a 'prod' after they've added it. 我希望函数在添加新产品时添加到“商品”数组中,并在某人决定添加后增加“产品”的“数量”时更新产品的现有数量。 It does this, until the 'prod' number reaches '10' and I don't know why. 它会执行此操作,直到“产品”编号达到“ 10”,而我不知道为什么。 I think it's getting confused between '1' and '10' but I don't know how to sort this out. 我认为它在'1'和'10'之间变得越来越混乱,但是我不知道如何解决。 Any ideas? 有任何想法吗?

function updateOrder(quant, prod, name, price, order) {

    var cnt = 0;

    //loop through the array to see what we have
    $.each(order.oitems, function(x, y) {

        //see if the product is already in the array   
        if ($.inArray(prod, order.oitems[x].product) !== -1) {

            // product is already in the cart, update with new quantities
            order.oitems.splice(x, 1, {
                "quantity": quant,
                "product": prod,
                "name": name,
                "price": price
            });
            cnt++;
            feedback("You already have this product. The quantities have been updated.");
        }

    });


    if (cnt == 0) {
        order.oitems.push({
            "quantity": quant,
            "product": prod,
            "name": name,
            "price": price
        });
        feedback("This product has been added to your order.");
    }
}

I suspect that you're passing a string to the parameter prod of your updateOrder function. 我怀疑您要将字符串传递给updateOrder函数的参数prod When you use $.inArray() on a string, it processes it as an array of individual characters. 在字符串上使用$.inArray()时,会将其作为单个字符数组进行处理。

More specifically, the string "10" is an array of two characters: 1 and 0. If you already have the product 1 in your array of items, then one of the tests performed is going to be this: $.inArray("10", "1") , which will pass because "1" is one of the characters that forms the string "10". 更具体地说,字符串“ 10”是两个字符组成的数组:1和0。如果您的商品数组中已经有产品1 ,则执行的测试之一将是: $.inArray("10", "1")会通过,因为” 1“是构成字符串” 10“的字符之一。

You can - and should - simplify your condition to the following: 您可以-并且应该-将您的情况简化为以下内容:

if(prod == order.oitems[x].product) {...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM