1

I have an array and I want to convert that array into a csv file here is my code and how i store my arrays :

const words = ['item1','item2','item3']

const txtNumber = 2;
let newArr = []

for(let t= 1 ; t <= txtNumber ; t++) {
    const data = "item1 , item2 , item3 ,item4";
    const findWord = (word) => data.split(",").filter(x => x.includes(word))
    const array = [];
    for(let i = 0; i < words.length ; i++) {
        const x = findWord(words[i]);
        array.push(x[0])
    }
    newArr = array.map(x => x === undefined ? '' : x.trim());
    console.log(newArr)
}

how can i store newArr values into items.csv into my pc ?

5
  • The output of your code is a 1D Array, can you give an example of the desired output format? (is it rows or columns etc?) Commented Oct 21, 2022 at 15:42
  • Looking at your code, I think you might want newArr.push() instead of newArr = (as you are over-writing the previous results.) Commented Oct 21, 2022 at 15:53
  • yea i tried to make it work like this to store each array into a new line inside the csv file Commented Oct 21, 2022 at 15:54
  • @NickSlash the push function , is pushing the whole previous array into a single array which will make the whole array in a single line inside the csv file Commented Oct 21, 2022 at 15:57
  • My point was that after your code executes there is only one row in newArr, if each line is added to the CSV inside the for loop this is not an issue. If you could give provide an example of the output you desire it would make it easier. Commented Oct 21, 2022 at 16:21

3 Answers 3

1

Im a little confused about what the desired output is, so no idea if this helps.

const fs = require('fs')
let writeStream = fs.createWriteStream('output.csv')

const words = ['item1','item2','item3']

const txtNumber = 2;
let newArr = []

for(let t= 1 ; t <= txtNumber ; t++) {
    const data = "item1 , item2 , item3 ,item4";
    const findWord = (word) => data.split(",").filter(x => x.includes(word))
    const array = [];
    for(let i = 0; i < words.length ; i++) {
        const x = findWord(words[i]);
        array.push(x[0])
    }
    newArr = array.map(x => x === undefined ? '' : x.trim());
    //console.log(newArr)
    writeStream.write(newArr.map(x=>`"${x}"`).join(",")+"\n")
}

writeStream.end()

The output of this should be (although I have not tested it)

"item1","item2","item3"
"item1","item2","item3"
Sign up to request clarification or add additional context in comments.

1 Comment

yes this is what i needed , it stores the data into an array then save it inside a csv file ♥
0

Store newArr values into items.csv

const fs = require('fs');

const words = ['item1','item2','item3']

const txtNumber = 2;
let newArr = []

for(let t= 1 ; t <= txtNumber ; t++) {


const data = "item1 , item2 , item3 ,item4";
const findWord = (word) => data.split(",").filter(x => x.includes(word))

const array = [];
for(let i = 0; i < words.length ; i++) {
    const x = findWord(words[i]);
    array.push(x[0])
}
newArr = array.map(x => x === undefined ? '' : x.trim());


console.log(newArr)}





const writeStream = fs.createWriteStream('items.csv');


writeStream.write(`item \n`);

writeStream.write('[ "' + newArr.join('","') + '" ]\n');

1 Comment

this one works but it only stores 1 row , i have 2 outputs and i want to store these 2 outputs into separate rows
0

Super lazy solution would be, for example, this NPM package found at:

https://www.npmjs.com/package/convert-array-to-csv

Disadvantage would be have an additional dependency to your project which you have no (or little) control over.

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.