2

我正在尝试创建一个函数来停止使用 Jquery 的 inArray 函数在数组中创建重复的子数组,但它似乎不起作用。也许我的代码中遗漏了一些东西?

function createProduct() {
    var name = document.getElementById("productname").value
    var myclass = document.getElementById("productclass").value
    var model = document.getElementById("model").value
    var manufacturer = document.getElementById("manufacturer").value
    var sellingprice = document.getElementById("productsellprice").value
    var costprice = document.getElementById("productcostprice").value
    var quantity = document.getElementById("quantity").value
    productobject = [name, manufacturer, model, myclass, sellingprice, costprice, quantity];

    var searchvalue = productobject;
    *if (jQuery.inArray(productobject, products) == -1) { products.push(productobject) } else if (jQuery.inArray(productobject, products) != -1) {
        document.getElementById
            ("mydiv").innerHTML = "This product already exists, please check catalogue to edit product";
    }*
}

我添加了代码块的其余部分以使其可读,但专注于调用 inArray 函数的 if 块,因为不知何故,仍然添加了重复的产品

4

2 回答 2

2

您用 if 条件询问对数组的引用是否已经在数组中,因为您总是在检查它们是否存在之前创建新的数组引用。

每次您productobject = [ ... ];创建一个新数组时,由于您推送的是对象(此处为数组)而不是原始值,因此该jQuery.inArray方法只能检查对象引用的存在。换句话说,由于您总是在创建一个新数组,因此您的 if 条件将始终找不到它。

编辑
我认为在您尝试做的事情的情况下,我可能会利用 Javascript 关联数组。这是我如何修改您的解决方案以使用对象而不是数组的示例。有关使用方法的更强大的object.hasOwnProperty方法,请参阅如何在 JavaScript 中检查对象是否具有属性?

最后,请注意我使用 productobject.name 属性作为 products 对象中的查找键,因此如果您更改第一个字段,单击该按钮只会添加一个新对象。

希望对你有帮助>>

//objects are associative arrays (or maps/dictionaries) in Javascript
var products = {}; //new Object() instead of new Array()

function createProduct() {
    var productObject = { 
        name: document.getElementById("productname").value,
        myclass: document.getElementById("productclass").value,
        model: document.getElementById("model").value,
        manufacturer: document.getElementById("manufacturer").value,
        sellingprice: document.getElementById("productsellprice").value,
        costprice: document.getElementById("productcostprice").value,
        quantity: document.getElementById("quantity").value
    }
    return productObject;
}

function checkitout() {
    var product = createProduct();
    //assuming name is a unique ID or key you can use to distinguish products
    if(products.hasOwnProperty(product.name)) {
        alert("product already exists");
    } else {
        console.log("new product added");
        products[product.name] = product;
    }
}
<input id="productname" value="IBM Lenovo 9439-CTO" />
<input id="productclass" value="desktop" />
<input id="model" value="9439-CTO" />
<input id="manufacturer" value="IBM Lenovo" />
<input id="productsellprice" value="$50.00" />
<input id="productcostprice" value="$30.00" />
<input id="quantity" value="1" />
<button onclick="checkitout();">Add New Product</button>

于 2015-07-08T21:04:34.063 回答
0

在我原来的答案中,我没有考虑到每个数组实际上都是一个描述。

所以这个实现会有点脆弱。但就@ThisClark 而言。你可以做类似的事情

var products = [ ["some product"...] ];

function createProduct() {
  ...


  for (var i = 0, len = products.length; i < len; i++) {
    if (jQuery.inArray(productobject[0], products[i] > -1) {
      //don't need to add
      break;
    } else {
      //need to add
    }
  }
}

这将测试 productobject 数组中的项目是否存在于 products 数组内的任何数组中。

或者,如果您有能力更新数据,最好为您的产品使用 json 数组。它可能会使使用单个产品结构的工作变得更加容易。

于 2015-07-08T21:06:38.123 回答