I did not quite understand what kind of basket. If you are unable to add data from some fields to an object, you can do something like this by using the product_id for example:
var cart = {} function addToCart() { var product_id = $('#product_id').val(); const currentProduct = 'product' + product_id; var articul = 'articul1' + product_id; var color = '#0cf'; // добавить данные в объект, ключ - ид продукта cart[product_id] = { 'currentProduct': currentProduct, 'articul': articul, 'color': color } $('#modal-basket').html($('#modal-basket').html() + '<p>Товар' + product_id + '</p>' + getHtml(cart[product_id])); console.log(cart); } function getHtml(arr) { var html = ''; html += 'Продукт: ' + arr.currentProduct + '<br />'; html += 'Артикул: ' + arr.articul + '<br />'; html += 'Цвет: ' + arr.color + '<br />'; return html; }
div#modal-basket { height: 400px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="product_id" value="1" /> <input type="button" id="addToCart" value="addToCart" onclick="addToCart()" /> <div id="modal-basket"></div>
UPD. Updated the answer for your comment. In your function, added the code for adding goods to the object cart. I hope I understood correctly and need it.
function addToCart(e) { const currentProduct = $(e.currentTarget).closest('.product'); var articul = $(this).attr('data-art'); var color = $('.btn-color.active', currentProduct).text().trim(); var quantity = $('.quantity-input', currentProduct).val(); ///////// var product_id = 'a'+articul+'c'+color; if (cart[product_id]) { // Если товар с выбранным цветом есть в коизине, обновим количество: cart[product_id]['quantity'] += quantity; } else { // Если нет - добавим новый товар в корзину: cart[product_id] = { 'articul' : articul, 'color' : color, 'quantity' : quantity } } //////////// document.getElementById('modal-basket').innerHTML = Object.values(cart); console.log(cart); showMiniCart(); }