Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

352
Vistas
Why does this code produce an empty dictionary and how can I fix it?

I am trying to read a file and split on multiple characters. If I put the log statement within the rl.on block I get some outputs but for some reason the dictionary is empty again when the block is done.

let rl = readline.createInterface({
        input: fs.createReadStream('Spanish.txt'),
    })
    

   let dict={};
   let arr1=[];
   let arr2=[];
   rl.on(`line`, (line)=>{
       if (!line.startsWith('#')){
           arr1=line.split('\t');
           if (arr1[1]!=undefined) {
               arr2 = arr1[1].split('[').join(',').split('/').
               join(',').split('(').join(',').split(',');
               dict[arr1[0]]=arr2[0];
           }
       }
    });
    console.log(dict);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The lines are read asynchronously, so by the time you log your dict, processing has not yet completed.

You can listen for the close event to determine whether the file has been fully read:

rl.on('close', () => console.log(dict));

Better yet, use an async/await-based approach as detailed in the Node.js documentation.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda