Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda