0

I am wondering what does this three dot operator do in this case:

const csv = [
            columns
            ...data
        ].map((row) => {
            return Object.values(row).join(",");
        }).join("\n");

I am trying to convert data to CSV, and if I assign data in the array without three dots operator like this:

const csv = [columns, data].map ...

It returns [object Object], while the one with three dot operators returns the correct data.

What does the operator do here?

I want to add a ternary operator but it seems like it's prohibited with a three dots operator.

what I want to achieve:

const csv = [
            columns
            (condition) ? ...anotherData : ...data
        ].map ...

1 Answers1

0

Move the spread syntax (...) before the conditional operator.

const csv = [columns, ...condition ? anotherData : data];
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 3
    I would write it as `...(condition ? anotherData : data)` so the precedence is stated explicitly, which make it easier for a human to parse. – kmoser Feb 03 '23 at 06:37