Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda