I am trying to get some data out of my arrays but I always seem to struggle with arrays. My data takes the following structure
{
tempAttachments: [{
_id: "12345-678910",
bytes: 412051,
file: 'File',
size: 411532,
title: "someFile.csv",
headers: ['optionOne', undefined, 'optionTwo', undefined, undefined, 'optionThree'],
type: "file",
fileType: "typeOne"
}, {
_id: "9999-2222",
bytes: 12345,
file: 'File',
size: 23456,
title: "anotherFile.csv",
headers: ['optionOne'],
type: "file",
fileType: "typeTwo"
}
]
}
I am trying the get the headers part out into its own array. The indexes are important, as they relate to something. I am also using the fileType as an indentifier. So my aim is to end up with something like this
[
{
"typeOne": [
"optionOne",
"optionTwo",
"optionThree"
]
},
{
"typeTwo": [
"optionOne"
]
}
]
As you can see I am ignoring the undefined options. So what I am currently trying is this
const mapping = {}
for (const attachment of this.tempAttachments) {
for (const [index, header] of attachment.headers.entries() || []) {
mapping[attachment.fileType] = [
index, header
]
}
}
Unfortunately, the results are as follows
[
{
"typeOne": [
0,
"optionOne"
]
},
{
"typeTwo": [
0,
"optionOne"
]
}
]
So how can I achieve the output I am after?
Thanks