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" data-id="id">2</div>
<div data-cost="40" class="item" data-id="test">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" data-id="test">6</div>
</div>
<script>
let items = document.querySelectorAll('.item[data-id]');
let result = {};
let obj = [...items].reduce((ob, {
dataset: {
cost,
id
}
}, i) => {
let ar = ob[id];
if (!ar) ob[id] = ar = [cost];
else {
ar.push(cost);
result[id] = ar;
}
return ob
}, {});
document.write(JSON.stringify(result))
</script>
</body>
</html>