Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
      }
    ]
  }
]

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading