I'm trying to create a function to stop the creation of duplicate sub arrays in an array using Jquery's inArray function but it doesnt seem to work. Maybe i am missing something in my code?
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";
}*
}
i have added the rest of the codeblock to make it readable but focus on the if block calling the inArray function because somehow, duplicate products still gets added
productsarray. I'm guessing theproductsarray is created outside of this scope, so it'd be interesting to see. I would start debugging by printing out yourproductsarray to the console in the if statement and see if it's what you expect it to be.var products = [ ["Test Product", "Test Manufacturer", "Test Model", "Test Class", "Test Sellingprice", "Test Costprice", "Test Quantity"] ]and i already debugged it, it would seem that the first if condition is never reached, so the products gets added either ways.productobjectexist in the arrayproductsthe answer is no,productsas defined has 1 object, which is an array. -- i also added an answer to make the point clearer. hope that helps.