Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
Is there a better way to extract the dataset from a nodelist?

I have the following list.

<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>

and I use the following code to get all of them in a list.

let tacos = document.querySelectorAll(".taco");

Then I use the following code to get all the datasets

let tacosArray = [];
for (let i = 0; i < tacos.length; index++) {
   tacosArray.push(tacos[i].dataset.id);
}

I now have an array of datasets, but there has to be a better way.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!