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

206
Vistas
every() es6 for nested array is not working?

I expect true for below usage of every but it wasn't, what's wrong with my logic?


const isAllChecked = [
    {
        "id": "1",
        "checked": true,
    },
    {
        "id": "2",
        "checked": true,
        "nested": [
            {
                "id": "2.1",
                "checked": true,
            },
            {
                "id": "2.2",
                "checked": true,
            }
        ]
    },
    {
        "id": "3",
        "checked": true,
    },
].every(
    (o) =>
      o.checked &&
      o.nested?.every((o) => o.checked)
  )

What I wanted: if any of the level 1 checked or nested checked is false then isAllChecked is false, but if non of the checked property in level 1 or nested is false, isAllChecked should return true.

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

0

Optional chaining will evaluate to undefined if the chain fails, so for the second iteration

(o) =>
  o.checked &&
  o.nested?.every((o) => o.checked)

will evaluate to

(o) =>
  true
  undefined

You probably wanted

(o) =>
  o.checked &&
  (!o.nested || o.nested.every((o) => o.checked))

or

(o) =>
  o.checked &&
  (o.nested || []).every(o => o.checked)

const isAllChecked = [
    {
        "id": "1",
        "checked": true,
    },
    {
        "id": "2",
        "checked": true,
        "nested": [
            {
                "id": "2.1",
                "checked": true,
            },
            {
                "id": "2.2",
                "checked": true,
            }
        ]
    },
    {
        "id": "3",
        "checked": true,
    },
].every(
    (o) =>
      o.checked &&
      (o.nested || []).every((o) => o.checked)
  )
console.log(isAllChecked);

about 4 years ago · Juan Pablo Isaza Denunciar

0

In the absence of a nested property, o.nested? will evaluate to a falsy value.

You could use a combination of every() and some():

const isAllChecked = [{
    "id": "1",
    "checked": true,
  },
  {
    "id": "2",
    "checked": true,
    "nested": [{
        "id": "2.1",
        "checked": true,
      },
      {
        "id": "2.2",
        "checked": true,
      }
    ]
  },
  {
    "id": "3",
    "checked": true,
  },
].every((o) => o.checked && !o.nested?.some((o) => !o.checked));

console.log(isAllChecked);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If an item doesn't contain the nested property then o.nested?.every((o) => o.checked) will evaluate to false, hence the every method would also return false.

You can instead use Array.prototype.some for the checking the items inside the nested array.

const isAllChecked = [
  { id: "1", checked: true },
  {
    id: "2",
    checked: true,
    nested: [
      { id: "2.1", checked: true },
      { id: "2.2", checked: true },
    ],
  },
  { id: "3", checked: true },
].every((o) => o.checked && !o.nested?.some((o) => !o.checked));

console.log(isAllChecked);

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