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

88
Views
Array filter returning entire array

I'm trying to filter an array with nested objects like this:

const items = [
    {
        "id": 1,
        "name": "test1",
        "subitems": [
            {
                "id": 2,
                "name": "test2",
                "subsubitems": [
                    {
                        "id": 3,
                        "name": "test3",
                    },
                    {
                        "id": 4,
                        "name": "test4",
                    }
                ]
            }
        ]
    },
    {
        "id": 10,
        "name": "test10",
        "subitems": [
            {
                "id": 20,
                "name": "test20",
                "subsubitems": [
                    {
                        "id": 30,
                        "name": "test30",
                    }
                ]
            }
        ]
    }
]

const filteredResults = items.filter((item) =>
    item.subitems.filter((subitem) =>
        subitem.subsubitems.filter((subsubitem) =>
            subsubitem.name.includes('test3')
        )
    )
)

console.log(filteredResults)

But it's not filtering correctly, the original array is being returned. Right now I'm just attempting to filter at the subsubitems level. Ultimately I want to return an array with any matches to name at any level.

So test2 would return an array like this:

[
    {
        "id": 1,
        "name": "test1",
        "subitems": [
            {
                "id": 2,
                "name": "test2",
                "subsubitems": [
                    {
                        "id": 3,
                        "name": "test3",
                    },
                    {
                        "id": 4,
                        "name": "test4",
                    }
                ]
            }
        ]
    }
]

And test3 would return an array like this:

[
    {
        "id": 1,
        "name": "test1",
        "subitems": [
            {
                "id": 2,
                "name": "test2",
                "subsubitems": [
                    {
                        "id": 3,
                        "name": "test3",
                    }
                ]
            }
        ]
    }
]

And test would return everything.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try This:

const items = [
  {
    id: 1,
    name: "test1",
    subitems: [
      {
        id: 2,
        name: "test2",
        subsubitems: [
          {
            id: 3,
            name: "test3",
          },
          {
            id: 4,
            name: "test4",
          },
        ],
      },
    ],
  },
  {
    id: 10,
    name: "test10",
    subitems: [
      {
        id: 20,
        name: "test20",
        subsubitems: [
          {
            id: 30,
            name: "test30",
          },
        ],
      },
    ],
  },
];

const filterItems = (items, filter) => {
  const result = [];
  items.forEach((item) => {
    if (item.name === filter) {
      result.push(item);
    }
    item.subitems.forEach((subitem) => {
      if (subitem.name === filter) {
        result.push(item);
      }
      subitem.subsubitems.forEach((subsubitem) => {
        if (subsubitem.name === filter) {
          result.push(item);
        }
      });
    });
  });

  console.log("filtered", result);
};

filterItems(items, "test10");

Result is :

filtered [ { id: 10, name: 'test10', subitems: [ [Object] ] } ]
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!