javascript - Jquery inArray method not working in multidimensional array -


i'm trying create function stop creation of duplicate sub arrays in array using jquery's inarray function doesnt seem work. maybe missing in 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 exists, please check catalogue edit product";     }* } 

i have added rest of codeblock make readable focus on if block calling inarray function because somehow, duplicate products still gets added

you're asking if condition whether reference array in array never because creating new array references before checking existence.

every time productobject = [ ... ]; you're creating new array , since you're pushing objects (array in this) rather primitive values, jquery.inarray method can check presence of object references. in other words, since you're making new array, not found if condition.

edit
i think in case of trying do, may take advantage of javascript associative arrays. here's example of how might rework solution use objects instead of arrays. stronger approach using object.hasownproperty method, please refer how check if object has property in javascript?

finally, please note used productobject.name property lookup key in products object, clicking button add new object if change first field.

hope helps >>

//objects 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 unique id or key can use distinguish products      if(products.hasownproperty(product.name)) {          alert("product 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>


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -