Home > database >  Adding an empty object between each line in a typescript array
Adding an empty object between each line in a typescript array

Time:02-28

I'm creating an iterator with the .map() function like so:

var csv = this.invoices
  .map(x => ({
    invoiceId: x.invoiceId,
    invoiceDate: x.invoiceDate,
    invoiceType: x.invoiceType,
    amount: x.subtotal,
  }));

I'm going to be exporting this array to a CSV, and need to have a blank line between items. The CSV helper doesn't do this, so how do I add in an empty object between each item in my csv array?

CodePudding user response:

One option would be to flatMap instead of map, and add an empty object to the front or back of the array, then pop or shift accordingly.

var csv = this.invoices
  .flatMap(x => [
    {},
    {
      invoiceId: x.invoiceId,
      invoiceDate: x.invoiceDate,
      invoiceType: x.invoiceType,
      amount: x.subtotal,
    }
  ]);
csv.shift();
  • Related