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

253
Views
Trying to filter out items from a deeply nested sub array of objects, keep getting property doesn't exist and empty array response

I have an array of objects each with its own subarray of objects.

I'm trying to filter through each of the subarrays and remove any element that doesn't match the condition expired === false but I keep getting an error that says: Property 'expired' does not exist on type '{ name: string; expired: boolean; }[]'.ts(2339) empty array and I'm not sure what I'm doing wrong.

Codesandbox

My function is this:

result.filter(files => files.files.expired === false)

Here's what the parent object structure looks like:

const result = 
[
  {
    type: "Documents",
    files: [
      {
        name: "file_1",
        expired: true
      },
      {
        name: "file_2",
        expired: false
      },
      {
        name: "file_3",
        expired: false
      },
      {
        name: "file_4",
        expired: true
      },
      {
        name: "file_5",
        expired: false
      }
    ]
  },
  {
    type: "Images",
    files: [
      {
        name: "file_1",
        expired: true
      },
      {
        name: "file_2",
        expired: false
      }
    ]
  }
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're filtering the result array, not the sub-arrays.

This will filter the files arrays in place in the result array

const result = [{
    type: "Documents",
    files: [{
        name: "file_1",
        expired: true
      },
      {
        name: "file_2",
        expired: false
      },
      {
        name: "file_3",
        expired: false
      },
      {
        name: "file_4",
        expired: true
      },
      {
        name: "file_5",
        expired: false
      }
    ]
  },
  {
    type: "Images",
    files: [{
        name: "file_1",
        expired: true
      },
      {
        name: "file_2",
        expired: false
      }
    ]
  }
];

result.forEach(el => el.files = el.files.filter(file => file.expired === false));
console.log(result)

This will create a new array of new objects:

const result = [{
    type: "Documents",
    files: [{
        name: "file_1",
        expired: true
      },
      {
        name: "file_2",
        expired: false
      },
      {
        name: "file_3",
        expired: false
      },
      {
        name: "file_4",
        expired: true
      },
      {
        name: "file_5",
        expired: false
      }
    ]
  },
  {
    type: "Images",
    files: [{
        name: "file_1",
        expired: true
      },
      {
        name: "file_2",
        expired: false
      }
    ]
  }
];

new_result = result.map(el => ({ ...el,
  files: el.files.filter(file => file.expired === false)
}))
console.log(new_result)

about 4 years ago · Juan Pablo Isaza Report

0

You have files.files as array , so you have to first loop over it and then do filter, something like

result.map((files) => files.files.filter(file=> file.expired === false));
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!