Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
How to create an array/object from an ordered list?

How can I create an array or object from an ordered list, with the number as the index/key and the list item as the corresponding value?

Example list:

<ol>
  <li>First list item</li>
  <li>Second list item</li>
  <li>Third list item</li>
</ol>

Should result in:

{"1" => "First list item", "2" => "Second list item", "3" => "Third list item"}

I tried using Array.from() with a few variations but that didn't work

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You could use querySelectorAll to get the <li>s and iterate over them to extract their innerText into an array:

const items = [...document.querySelectorAll('li')].map(x => x.innerText);

console.log(items);
<ol>
  <li>First list item</li>
  <li>Second list item</li>
  <li>Third list item</li>
</ol>

The resulting array will have item[0] === 'First list item', etc.

about 4 years ago · Santiago Trujillo Denunciar

0

First, write a query selector to get the <ol> tag and then read its children:

let ol = document.querySelector('ol')
let lis = ol.children

Then, define an object and aggregate all the <li> texts into it:

let obj = {}
for (let i = 0; i < lis.length; i++) {
  obj[i.toString()] = lis[i].innerText
}

That should create the object you're looking for.

about 4 years ago · Santiago Trujillo Denunciar

0

One of the way to do that:

let bob = Array.from( document.querySelector('ol').children, li => li.textContent )

console.log( bob)
<ol>
  <li>First list item</li>
  <li>Second list item</li>
  <li>Third list item</li>
</ol>

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda