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

128
Views
Cree una matriz a partir de salidas de bucle 'for of'

Tengo una declaración for of en JS que devuelve ID de elementos HTML pero están en múltiples salidas. Me gustaría tomar estas salidas y ponerlas en una matriz.

Por ejemplo, el HTML es:

 <div>
 <button id="savebtn-1"></button>
</div>
<div>
 <button id="savebtn-2"></button>
</div>

y mi JS es:

 const elements = document.querySelectorAll("button");
for (const element of elements) {
 let id = elements.getAttribute("id");
}

console.log(id) mostrará:

savebtn-1

savebtn-2

¿Cómo puedo obtener estas salidas en una matriz?

Gracias por cualquier atención que pueda dar a esta pregunta.

almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Haga otra matriz y almacene todas las identificaciones dentro de esa matriz.

 const resultArray = [];
const elements = document.querySelectorAll("button");

for (const element of elements) {
 const id = element.getAttribute("id");
 resultArray.push(id);
}
console.log(resultArray);
 <div>
<button id = "savebtn-1">Button 1</button>
</div>
<div>
<button id = "savebtn-2">Button 2</button>
</div>

almost 4 years ago · Santiago Trujillo Report

0

Use Array.from() en NodeList y un mapeador personalizado para extraer las ID

 const idArray = Array.from(
 document.querySelectorAll("button"),
 ({ id }) => id
);

console.log(idArray);
 <div>
 <button id="savebtn-1">Button #1</button>
</div>
<div>
 <button id="savebtn-2">Button #2</button>
</div>

almost 4 years ago · Santiago Trujillo Report

0

Puede crear una matriz vacía e insertar id s en esa matriz.

 const elements = document.querySelectorAll("button");

const idArray = [];

for (const element of elements) {
 let id = elements.getAttribute("id");
 idArray.push(id);
}

console.log('idArray ', idArray);
almost 4 years ago · Santiago Trujillo 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!