JSFiddle - React, Tailwind, and code Playground

by joshmoto

JavaScript

var product_id = 43;
var product_qty = 1;

basket = false;

// if product exists in basket
if (basket) {

	console.log('has items');

  // quantity vars
  var current_qty = parseInt(basket[product_id].qty);
  var updated_qty = current_qty + product_qty;

  // update item quantity
  basket[product_id] = {
    qty: updated_qty
  };
  
} else {

	console.log('is empty');
  
  basket = {};

  // add new item and quantity
  basket[product_id] = {
    qty: product_qty
  };

  console.log(basket);


  

}