I'm dealing with an array structured like this:
const AllFiles = [
{name: "ExistingFile1", mimetype: "image/jpg", size: "500"},
[
File, // This is the New File itself (see File API)
{name: "NewFile1", mimetype: "video/mp4", size: "9000"}
],
{name: "ExistingFile2", mimetype: "video/mp4", size: "4000"},
[
File, // This is the New File itself (see File API)
{name: "NewFile2", mimetype: "image/jpg", size: "500"}
],
]
The AllFiles
array includes a mix of objects and arrays.
I am trying to filter the AllFiles
array to only retrieve items where the mimetype
begins with "image/"
.
This is what I started with:
const FilteredFiles = AllFiles.filter((FileItem, index) => FileItem.mimetype.startsWith("image/"))
The issue is that it only checks for the mimetype
in top level objects. It does not check deeper where a New File has the mimetype
within a second level object inside an array.
To access the mimetype
of existing files you must use: FileItem[0].mimetype
However, to access the mimetype
for new files you need to use: FileItem[0][1].mimetype
Due to this inconsistency, I am struggling to filter the AllFiles
array. Is there a way to solve this?