3

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?

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I needed, thank you!
I like this! I'm embarrassed that my first thought was using iterators. ( developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ) so that you're not creating yet another array, but while it would be better "in theory", simple beats clever every time!

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.