title: JS Split ( ) to check if substring exists in Array
body: <p>I have an <code>array</code> of products that are stored as <code>Strings</code> in this format <code>productname:quantity</code>. The issue I am running into is that if a user adds one product with a quantity of x it is inserted into the <code>array</code> as it should. However, if they then decide to add more of a particular product a new entry is made into the <code>array</code> instead of checking if the product already exists and adjusting the quantity to the new value. <code>oldQty + newQty</code>. </p>
  
  <p>For example this is my array:</p>
  
  <pre><code>["CBL202659/A:1","OUTER9:1","PALLET CARDS:1"]
  </code></pre>
  
  <p>If I add another PALLET CARDS product it creates a new entry rather than updating the quantity of the existing item to 2.</p>
  
  <p>New array</p>
  
  <pre><code>["CBL202659/A:1","OUTER9:1","PALLET CARDS:1","PALLET CARDS:1"]
  </code></pre>
  
  <p>I would like the array to end up like this: - updating the quantity</p>
  
  <pre><code>["CBL202659/A:1","OUTER9:1","PALLET CARDS:2"]
  </code></pre>
  
  <p>Currently this is my code:</p>
  
  <p>I use the <code>split()</code> method to seperate the String where a colon occurs and store the product name and quantity in two seperate variables. </p>
  
  <pre><code>$(".orderBtn").click(function(event){
  
          //Show the order Box
          $(".order-alert").show();
          event.preventDefault();
  
          //Create the Array
          var productArray = [];  
  
          //Get reference to the product clicked
          var stockCode = $(this).closest('li').find('.stock_code').html();       
          //Get reference to the quantity selected
          var quantity = $(this).closest('li').find('.order_amount').val();
          var item = stockCode + ":" + quantity;
          var itemCheck = stockCode + ":";
  
          if(quantity == 0){
              console.log("Quantity must be greater than 0")
          }else{
              //If no Cookie exists, create one and add the Array
              if ($.cookie('order_cookie') === undefined) {
                  console.log("CREATE NEW COOKIE");
                  //Add items to Array
                  productArray.push(item);
                  //Add Array to Cookie
                  $.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
  
              //If the Cookie already exists do this  
              } else {
                  productArray = JSON.parse($.cookie('order_cookie'));//get ref to array
  
                  if(productArray.indexOf(itemCheck)!= -1){//It exists so update qty
                  console.log("EXISTS... updating item: " + itemCheck);
                              //var index = productArray.indexOf(item);
                              //var update = productArray[index].split(":");
                              //var name = update[0];
                              //var oldQty = update[1];
                              //console.log(name + ":" + oldQty);
                              //productArray[index] = item;
                  }else{//It does not exist, so add to array
                  console.log("Does not exist... adding new item: " + item);
                   //Append items onto the Array
                   productArray.push(item);
                  }
  
                  //Update the Cookie
                  $.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
                  console.log($.cookie('order_cookie'));
              }
  
              //Display the number of items in the Array in the Order Box
              $('#order_counter').html(productArray.length);
          }
      });
  </code></pre>
  
  <p>I suppose the real question I am asking here, is if it is possible to search the <code>array</code> for a <code>subString</code> - containing <code>productname:</code> ??</p>
  
  <p>After taking on board some of the answers and advice I've slightly modified how I am structuring my <code>array</code>.</p>
  
  <p>However I am running into issues regarding the name value pair.</p>
  
  <p>Step 1: Get references to the stockCode and Quantity</p>
  
  <pre><code>            //Create the Array
          var productArray = [];
          //Get reference to the product clicked
          var stockCode = $(this).closest('li').find('.stock_code').html();
          //Get reference to the quantity selected
          var quantity = $(this).closest('li').find('.order_amount').val();
  </code></pre>
  
  <p>print them in console to make sure they are correct</p>
  
  <pre><code>PALLET CARDS
  2 
  </code></pre>
  
  <p>Step 2: Insert them into the Array</p>
  
  <pre><code>    productArray.push({stockCode:quantity});
  </code></pre>
  
  <p>print them to make sure they are correct - NO - Problem here...</p>
  
  <pre><code>[Object]
  0: Object
  stockCode: "2"    &lt;---------------------- Should be PALLET CARDS: "2"
  __proto__: Object
  length: 1
  __proto__: Array[0]
  </code></pre>