ureech,
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<div class="group">
<div data-cost="0" class="item">1</div>
<div data-id="id" data-cost="10" class="item">2</div>
<div data-cost="20" class="item">3</div>
</div>
<div class="group">
<div data-cost="0" class="item">1</div>
<div data-cost="30" class="item">2</div>
<div data-cost="40" class="item">3</div>
</div>
<div class="group">
<div data-cost="0" class="item show">4</div>
<div data-cost="50" data-id="id" class="item show">5</div>
<div data-cost="60" class="item show">6</div>
</div>
<script>
let items = document.querySelectorAll('.item[data-id]');
let obj = [...items].reduce((ob, {
dataset: {
cost,
id
}
}, i) => {
let ar = ob[id];
if (!ar) ob[id] = ar = [cost];
else {
ar.push(cost);
let txt = `Что вам нужно я не понимаю, но есть ${ar.length} элемента с id = ${id} с cost = ${ar}`
alert(txt)
}
return ob
}, {});
</script>
</body>
</html>