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

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

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 Report

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 Report

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