0

How to detect if selected product code exist in MY CART, if the product code exist, do not insert it into MY CART array, instead add the quantity of the selected product into the existing product in MY CART.

The code I wrote get to add in all the product selected into MY CART, which is not quite efficient to see all the same product get piled up. It will be better to have this feature to sum the quantity if the product exist.

var cartlist= new Array();
$(".addtocart").on('click', function() {
   var prdcode = $("#prod_code");
   var description = $("#prod_desc");
   var qty= $("#prod_qty");
   var itemlist = new Array();

   itemlist.push(prdcode );
   itemlist.push(description );
   itemlist.push(qty);

   cartlist.push(itemlist);

   for (var i = 0; i < cartlist.length-1; i++) {
      if(cartlist[i+1] === cartlist[i]){
         continue;
         //action to sum qty if code exist in cart 
      }
   }
});
MY CART
0: Array(3){
     0: "ABC123"
     1: "DESCRIPTION FOR ABC123"
     2: "1"
}
1: Array(3){
     0: "DEF456"
     1: "DESCRIPTION FOR DEF456"
     2: "2"
}
TO BE ADDED IN CART
0: Array(3){
     0: "ABC123"
     1: "DESCRIPTION FOR ABC123"
     2: "1"
}
1: Array(3){
     0: "GHI789"
     1: "DESCRIPTION FOR GHI789"
     2: "1"
}
EXPECTED OUTCOME (AFTER ADDED IN CART)
0: Array(3){
     0: "ABC123"
     1: "DESCRIPTION FOR ABC123"
     2: "2"
}
1: Array(3){
     0: "DEF456"
     1: "DESCRIPTION FOR DEF456"
     2: "2"
}
2: Array(3){
     0: "GHI789"
     1: "DESCRIPTION FOR GHI789"
     2: "1"
}
Jeff
  • 59
  • 6
  • Loop over the cart, array find(), if find finds the array, increase. If it does not push it. – epascarello Oct 05 '21 at 04:52
  • Plenty of [solutions here](https://stackoverflow.com/search?q=%5Bjavascript%5D+merge+arrays) while [this answer](https://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects) seems closest to what you need with some minor tweaks. – freedomn-m Oct 05 '21 at 05:40

1 Answers1

1

The logic should be, before you push item to cart check if the item already exist in cart. If exist update it, if not push it.

If you are usng a for loop, you have to do it in two loops

Working Code

var cartlist = new Array();
cartlist.push(["ABC123", "DESCRIPTION FOR ABC123", "1"]);
cartlist.push(["DEF456", "DESCRIPTION FOR DEF456", "2"]);

var itemlist = new Array();
itemlist.push(["ABC123", "DESCRIPTION FOR ABC123", "1"]);
itemlist.push(["GHI789", "DESCRIPTION FOR GHI789", "1"]);

$(".addtocart").on('click', function () {
  // Donot push directly without checking
  // cartlist.push(itemlist);
  for (var i = 0; i < itemlist.length; i++) {
    let isFound = false; 
    for (var j = 0; j < cartlist.length && !isFound; j++) {
      if(cartlist[j][0] === itemlist[i][0] && cartlist[j][1] === itemlist[i][1]) {
        cartlist[j][2] = (+cartlist[j][2] + +itemlist[i][2]).toString();
        isFound = true;
      }
    }
    if(!isFound) {
      cartlist.push(itemlist[i]);
    }
  }
  console.log(cartlist);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<button class="addtocart">Add</button>

You could simply do it with Array.forEach and Array.find aswell.

var cartlist = new Array();
cartlist.push(["ABC123", "DESCRIPTION FOR ABC123", "1"]);
cartlist.push(["DEF456", "DESCRIPTION FOR DEF456", "2"]);

var itemlist = new Array();
itemlist.push(["ABC123", "DESCRIPTION FOR ABC123", "1"]);
itemlist.push(["GHI789", "DESCRIPTION FOR GHI789", "1"]);

$(".addtocart").on('click', function () {
  // Donot push directly without checking
  // cartlist.push(itemlist);
  itemlist.forEach((item) => {
    const itemFromCart = cartlist.find((node) => node[0] === item[0] && node[1] === item[1]);
    if(itemFromCart) {
      itemFromCart[2] = (+itemFromCart[2] + +item[2]).toString();
    } else {
      cartlist.push(item);
    }
  })
  console.log(cartlist)
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
<button class="addtocart">Add</button>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • Minor tweak: change `isFound = true;` to `isFound = true; break;` - with small arrays this won't make much difference, but with10s of 1000s of rows it can help a lot if there are a lot of duplicates. – freedomn-m Oct 05 '21 at 06:03
  • Same I implemented in a different way. I used the condition for for as `j < cartlist.length && !isFound;`. So once an item is found, the loop will be exited. Typically, I'm against using jump statements. – Nitheesh Oct 05 '21 at 06:55