I'm not sure what your goal is but you can get all of the data attributes in an array by selecting the elements, setting up an array for the data attributes and looping through the elements, placing the values in your array.
let carts = document.querySelectorAll('.addtocart');
const dataattr = [];
carts.forEach(el => {
dataattr.push({
product_name: el.dataset.name,
product_tag: el.dataset.name,
product_price: el.dataset.price,
product_img: el.dataset.img,
})
});
console.log(dataattr);
.addtocart:before {
content: attr(data-name);
font-family: sans-serif;
}
<div class="addtocart" data-name="apple" data-price="1.00" data-img="apple.jpg"></div>
<div class="addtocart" data-name="banana" data-price="0.70" data-img="banana.jpg"></div>
<div class="addtocart" data-name="orange" data-price="2.00" data-img="orange.jpg"></div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…