I have an array which is 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"}
],
]
As you can see the AllFiles
array contains a mix of objects and arrays.
I need to filter the AllFiles
array to only return items where the mimetype
starts with "image/"
.
So I started off with the following:
const FilteredFiles = AllFiles.filter((FileItem, index) => FileItem.mimetype.startsWith("image/"))
The problem is that it only looks for the mimetype
where it is in a top level object. Its does not look deeper where a New File has mimetype
in the second level object within an array.
To get the mimetype
of existing files you need to use: FileItem[0].mimetype
But to get the mimetype
for new files you need to use: FileItem[0][1].mimetype
Because of this mismatch I am unable to filter the AllFiles
array. How could this possibly be achieved?