i want to convert a .csv file and write a new one. However I am not able to remove the first , i am kinda stuck here and it is driving me crazy. This is my code:
JavaScript
x
8
1
var extractedtasks = tasks.slice(0, 3)
2
var extractedtasksformated = extractedtasks.toString().replace(/,$/g, "n")
3
4
let csvformat = "EMAIL,PASSWORD,MAILBOX"
5
fs.writeFileSync(tasklocation[0], csvformat + "n" + extractedtasksformated.replace(/,^/g,
6
""))
7
console.log(chalk.green("Successfully updated the CSV file"))
8
That’s the output i am getting in the newly generated file
JavaScript
1
5
1
EMAIL,PASSWORD,MAILBOX
2
example1@gmail.com,Password123,example@gmail.com:password
3
,example2@gmail.com,Password123,example@gmail.com:password
4
,example3@gmail.com,Password123,example@gmail.com:password
5
Output extractedtasks:
JavaScript
1
6
1
[
2
'example1@gmail.com,Password123,example@gmail.com:passwordr',
3
'example2@gmail.com,Password123,example@gmail.com:passwordr',
4
'example3@gmail.com,Password123,example@gmail.com:passwordr'
5
]
6
Output extractedtasksformated:
JavaScript
1
2
1
,example3@gmail.com,Password123,example@gmail.com:passwordxample@gmail.com:password
2
Advertisement
Answer
Because extractedtasks
is an array, instead of converting it to a string you should just join it with the expected separator:
JavaScript
1
16
16
1
extractedtasks = [
2
'example1@gmail.com,Password123,example@gmail.com:passwordr',
3
'example2@gmail.com,Password123,example@gmail.com:passwordr',
4
'example3@gmail.com,Password123,example@gmail.com:passwordr'
5
]
6
7
extractedtasksJoined = extractedtasks.join("n")
8
// "example1@gmail.com,Password123,example@gmail.com:passwordrnexample2@gmail.com..."
9
10
// depending on the target line separator, you should also probably
11
// remove the "r"
12
extractedtasksJoined = extractedtasksJoined.replace("r", "")
13
14
// finally
15
fs.writeFileSync(tasklocation[0], csvformat + "n" + extractedtasksJoined + "n")
16