Home > front end >  How to detect same element in array and sum up qty?
How to detect same element in array and sum up qty?

Time:10-05

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"
}

CodePudding user response:

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>

  • Related