Is there a better way to extract the dataset from a nodelist? [closed]
One possibility is Array.from
with its mapper function.
const tacos = Array.from(
document.querySelectorAll('.taco'),
div => div.dataset.id
);
console.log(tacos);
<div class="taco" data-id="2"></div>
<div class="taco" data-id="3"></div>
<div class="taco" data-id="4"></div>
<div class="taco" data-id="23"></div>
<div class="taco" data-id="55"></div>
The spread operator ...
and .map()
const ids = [...document.querySelectorAll('.taco')].map(id => id.dataset.id);
console.log(JSON.stringify(ids));
<div class="taco" data-id="2"></div>
<div class="taco" data-id="3"></div>
<div class="taco" data-id="4"></div>
<div class="taco" data-id="23"></div>
<div class="taco" data-id="55"></div>