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

186
Views
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
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!