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

271
Vistas
Alternative for `some` method in NodeList

I have a simple question, what simplest method do you know to replace the method some for NodeList? This some method is only for Array prototype.

I have currently come up with the rather outlandish idea of iterating and holding a boolean in a variable, however I would like to know your point of view.

My obscure approach

const toCheck = (elem) => elem.checked;

const methodSome = (list, fn) => {
  let result = false;
  for (let i = 0; i < list.length; i++) {
    if (fn(list[i])) {
      result = true;
      break;
    }
  }
  return result;
};

const elems = document.querySelectorAll(`[name="something"]`);

const hasBeenChecked = methodSome(elems, toCheck);

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

As others have mentioned, the spread operator can put elements into an array.

A simpler approach is to use the :checked pseudo-class selector with a call to document.querySelector() (instead of using document.querySelectorAll()). The Double NOT operator !! can be used to convert the element to a boolean.

const hasBeenChecked = !!(document.querySelector(`[name="something"]:checked`));

With this approach there is no need to put elements into an array and have a function with a loop to check those elements.

See an example here:

const hasBeenChecked = !!(document.querySelector(`[name="something"]:checked`));
console.log('hasBeenChecked: ', hasBeenChecked);
<div><input type="checkbox" name="something" /> something?</div>
<div><input type="checkbox" name="something" checked /> something?</div>
<div><input type="checkbox" name="something" /> something?</div>
<div><input type="checkbox" name="something" checked /> something?</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use the spread operator to convert a NodeList to an Array

const hasBeenChecked = [...elems].some(toCheck);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Convert it to an array first:

With spread operator:

const toCheck = (elem) => elem.checked;

const methodSome = (list, fn) => {
  let result = false;
  for (let i = 0; i < list.length; i++) {
    if (fn(list[i])) {
      result = true;
      break;
    }
  }
  return result;
};

const elems = document.querySelectorAll(`[name="something"]`);

const hasBeenChecked = [...elems].some(toCheck);

With Array.from

const toCheck = (elem) => elem.checked;

const methodSome = (list, fn) => {
  let result = false;
  for (let i = 0; i < list.length; i++) {
    if (fn(list[i])) {
      result = true;
      break;
    }
  }
  return result;
};

const elems = document.querySelectorAll(`[name="something"]`);

const hasBeenChecked = Array.from(elems).some(toCheck);

about 4 years ago · Juan Pablo Isaza 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