1

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

0

5 Answers 5

2

You may use Array.prototype.map() together with Array.prototype.filter() to get rid of undefind's inside header:

const src = {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"}]},

      result = src
        .tempAttachments
        .map(({headers,fileType}) => ({
          [fileType]: headers.filter(Boolean)
        }))

      
console.log(result)      
.as-console-wrapper{min-height:100%;}

Sign up to request clarification or add additional context in comments.

3 Comments

How comes you changed your answer? The first one was actually correct, as it also displays the required index.
I have appended filtering out undefined from headers, not it provides your expected output exactly
Your first answer seemed correct. I was not getting any undefined values and it contained the index
1

Nothing a simple map and filter couldn't do

const arrays = [
  {headers: ["optionOne", undefined, "optionTwo", undefined, undefined, "optionThree"], fileType:"typeOne"},
  {headers: ["optionOne"], fileType:"typeTwo"},
]

function filter(option){
  return option !== undefined
}

const result = arrays.reduce((result, element) => ({...result, [element.fileType]: element.headers.filter(filter)}), {})

console.log(result)

Comments

1

Use map and filter

const data = [
  {
    headers: [
      "optionOne",
      undefined,
      "optionTwo",
      undefined,
      undefined,
      "optionThree",
    ],
    fileType: "typeOne",
  },
  { headers: ["optionOne"], fileType: "typeTwo" },
];
const res = data.map(({ headers, fileType }) => ({
  [fileType]: headers.filter(Boolean),
}));

console.log(res);

1 Comment

this will remove all falsey values, not only undefined
0

You can try

tempAttachments.map(
  ({headers,fileType}) => {
    return ({[fileType]: headers.filter((val) => val)})
  }
)

Comments

0

const data = [{_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',},];;

const result = data.map(d => {
  let headers = [];
  d.headers.forEach((h, index) => {
    if (!(h === undefined || h === null)) headers.push({ [index]: h });
  });

  return {
    [d.fileType]: [...headers],
  };
});
console.log(result);
.as-console-wrapper{min-height:100%;}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.