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

133
Views
How to push data that contains specific column?

I am writing a NodeJS script that reads this huge

and I need to write new file that contains only rows of data that have valid numeric profit values and then order the data based on profit value, and

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've used the mapHeaders option to convert the column headings to lowercase, and just use the first word (so we discard "(in millions)").

Then if the profit column exists, I parse it to a float. Then we can later use that in sort() to get the top 20 rows by profit.

Instead of saving all the rows in results, I just increment a variable containing a line counter.

const fs = require("fs");
const csv = require("csv-parser");
let lines = 0;
const validResults = [];
const inputFile = "./data.csv";
fs.createReadStream(inputFile)
  .pipe(csv({
    mapHeaders: h => h.split(" ")[0].toLowerCase(),
  }))
  .on("data", (data) => {
    try {
      if (data.profit) {
        data.profit = parseFloat(data.profit);
        validResults.push(data);
      }
      lines++;
    } catch (err) {
      console.log(err)
    }
  })
  .on("end", () => {
    console.log('# of Rows of data is', lines);
    fs.writeFile('data2.json', JSON.stringify(validResults.sort((a, b) => b.profit - a.profit).slice(0, 20)), (err) => {
      if (err) throw err;
      console.log("File created")
    })
  });

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!