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

274
Views
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 answers
Answer question

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 Report

0

Use the spread operator to convert a NodeList to an Array

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

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