Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
How to add character to the end of the line in node js or javascript

I have a file that has 6,000 of json. Its raw data and I need to add an comma ' , ' to the end of each line. Can someone help. Current code is creating a new line with a comma I need comma at end of each line. Node js or Javascript example is acceptable

The data file:

{"id": "67", "ac": []}
{"id": "78","ac": []}
{"id": "90", "ac": []}

What I am currently getting:

{"id": "67", "ac": []}
,
{"id": "78","ac": []}
,
{"id": "90", "ac": []}

What I need

{"id": "67", "ac": []},
{"id": "78","ac": []},
{"id": "90", "ac": []},

Current code

"use strict";

const fs = require("fs");

fs.readFile("users.json", (err, data) => {
  if (err) throw err;
  let student = data.toString();

  var lines = student.split(/(\n|\r\n)/);

  var new_content = lines
    .map(function (line) {
      return line + ",";
    })
    .join("\r\n");
  fs.writeFile("newFile.json", new_content, (err) => {
    if (err) throw err;
    console.log("Data written to file");
  });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Looking at your code, it looks like your trying to mutate a string by adding the comma in the map function. 'line' is immutable so you cant add the comma, but you can reassign the value; I.E line = line += ',' or newValue = line += ',' then return the value after that.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!