JSFiddle - React, Tailwind, and code Playground
HTML
Product 1: <button id="product1">Add one</button>
<br>
Product 2: <button id="product2">Add one</button>
<div></div>
JavaScript
var cart = [];
$(document).ready(function(){
$('button').on('click', function() {
cart.push( { id:$(this).attr('id'), qty:1 } );
var result = cart.reduce(function(res, obj) {
if (!(obj.id in res )) {
res[obj.id] = 0
}
return ( res[obj.id] += obj.qty ) && res ;
}, {} )
console.log( result )
});
});