1
const fs = require('fs');
const csv = require('fast-csv');

let ws = fs.createWriteStream('/Users/emreakyol/Desktop/column.csv');

let part1 = ["C1R1", "C1R2", "C1R3"];
let part2 = ["C2R1", "C2R2", "C2R3"];
let part3 = ["C3R1", "C3R2", "C3R3"];
csv.
    write([
        part1,
        part2,
        part3
    ], {headers:true})
        .pipe(ws);

I would like to see like that into my.csv file. Output(my.csv file):

C1R1 C2R1 C3R1
C1R2 C2R2 C3R2
C1R3 C2R3 C3R3

I saw "Arrays of array" about fast-csv library but I could not find how to use it. https://www.npmjs.com/package/fast-csv

1 Answer 1

2

From what github issues says this functionality is not working (just report, no response).

Using syntax provided in questsion we are inserting arrays as rows. You want to insert arrays as coulmns so the workaround is some sort of a mapping function eg. (I've modified your code)

let part1 = ["C1R1", "C1R2", "C1R3"];
let part2 = ["C2R1", "C2R2", "C2R3"];
let part3 = ["C3R1", "C3R2", "C3R3"];
let parts = [part1, part2, part3];
parts = parts.map((part, index) => {
  let newPart = [];
  [...Array(part.length).keys()].forEach((iterator, innerIndex) => newPart.push(parts[innerIndex][index]))
  return newPart;
})
csv.write(parts, {headers:true})
Sign up to request clarification or add additional context in comments.

1 Comment

I got it! Thanx!!

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.