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

190
Visualizações
Pushing edited data to array returning original data in forEach loop

I'm trying to read an .html document using node, creating an array with said document, then attempting to iterate thru the array to look for placeholders that are in the document to swap data from an object that was created from a .csv read file.

When I console.log the array after running, it shows the information as though it's never been edited with the forEach loop. Am I missing something with async code?

Here it is:

import * as fs from 'node:fs';
import { parse } from 'csv-parse';
 

fs.readFile('test.csv', 'utf8', (err, data) => {
        let matrix = [];
        if (err) throw err;

        parse(data, {columns: true, trim: true}, function(err, rows) {
            if (err) throw err;
            rows.forEach(row => {
                // console.log(row)

                const newCode = []
                for (let [key, value] of Object.entries(row)) {
                    // console.log(key)

                    const keyToSearch = `{(${key})}`

                    try {
                        const data = fs.readFileSync('index.html', 'utf8')
                        const dataArr = data.split('\n')
                        
                        
                        dataArr.forEach(line => {
                            if (!line.includes(keyToSearch)) {
                                newCode.push(line)
                            } else {
                                let newLine = line.replace(keyToSearch, value)
                                newCode.push(newLine)
                            }
                        })
                        
                    }catch(err) {
                        console.error(err)
                    }
                    
                    
                }
                console.log(newCode)


                

            })
        })

    })
about 4 years ago · Juan Pablo Isaza
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