Jquery inArray 方法在多维数组中不起作用

Jquery inArray method not working in multidimensional array

我正在尝试创建一个函数来停止使用 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 块上,因为不知何故,仍然添加了重复的产品

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

每次你做 productobject = [ ... ]; 你都在创建一个新数组,因为你是在推送对象(这里是数组)而不是原始值,所以 jQuery.inArray 方法只能检查对象引用的存在。换句话说,因为你总是在创建一个新数组,所以你的 if 条件总是找不到它。

编辑
我认为在您尝试做的情况下,我可能会利用 Javascript 关联数组。这是一个示例,说明我如何修改您的解决方案以使用对象而不是数组。关于使用object.hasOwnProperty方法的更强方法,请参考How do I check if an object has a property in JavaScript?

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

希望对您有所帮助>>

//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=".00" />
<input id="productcostprice" value=".00" />
<input id="quantity" value="1" />
<button onclick="checkitout();">Add New Product</button>

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

所以这个实现会有点脆弱。但就@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 数组。它可能会使使用单个产品结构的工作变得容易得多。