繁体   English   中英

从目录读取文件并保存到 object 数组

[英]Read files from directory and save to array of object

我有一个员工税务档案的目录。 每个文件都有一个文件名作为员工代码。 我正在读取每个文件并提取一些组件并保存到一组员工对象中。

const readline = require('readline');
let empArr = [];

function readFiles(dirname) {
fs.readdir(dirname, async function (err,filenames) {

if(err) {
  return err;
}

for await (file of filenames) {
  const filePath = path.join(__dirname,directoryPath,file);
  const readStream =  fs.createReadStream(filePath);   
  const fileContent = readline.createInterface({
    input: readStream
  });    
 
  let employeeObj = {
    empId : '',
    TotalEarning:'',
    ProfessionalTax:0,
    GrossIncome:0,
    isDone:false
  };

  fileContent.on('line', function(line) {         
    if(!employeeObj.empId &&  line.includes("Employee:")) {    
      const empId = line.replace('Employee: ','').split(" ")[0];    
      employeeObj.empId = empId;
    }
    else if(line.includes('Total Earnings')) {
      const amount = line.replace(/[^0-9.]/g,'');      
      employeeObj.TotalEarning = amount;
    } 
    else if(line.includes('Profession Tax')) {        
      const amount = line.split(" ").pop() || 0;    
      employeeObj.ProfessionalTax = amount;
    } 
    else if(line.includes('Gross Income')) {
      const amount = line.replace(/[^0-9.]/g,'');      
      employeeObj.GrossIncome = amount ||0;
    } 
    else if(line.includes('finance department immediately')) {
      employeeObj.isDone =true;
      empArr.push(employeeObj);           
    }
  });

  fileContent.on('close', function() {
    fileContent.close();      
  });
  }    
 })
}

readFiles(directoryPath);

我无法获得 empArr。 拿到数组后需要保存到excel。 这部分我将在获取员工对象数组后尝试。

在阅读了几篇关于闭包和承诺的文章后,我得到了它的工作。 以下代码适用于我,并向我发送已处理的员工数组。

const directoryPath = './tax/';

function readFiles(dirname) {
  fs.readdir(dirname, async function (err,filenames) {    
   if(err) {
     return err;
   }

  let promiseArr = filenames.map( file=> {
    return new Promise((resolve)=>{
      processFile(file, resolve)
    })
 });

 Promise.all(promiseArr).then((ret)=>console.log(ret));   
 })
}
 

function processFile(file, callback) {
     const filePath = path.join(__dirname,directoryPath,file);
     const readStream =  fs.createReadStream(filePath);   
     const fileContent = readline.createInterface({
      input: readStream
     });    
  
    let employeeObj = {
      empId : '',
      TotalEarning:'',
      ProfessionalTax:0,
      GrossIncome:0,
      isDone:false
    };

   fileContent.on('line', function(line) {         
    if(!employeeObj.empId &&  line.includes("Employee:")) {    
      const empId = line.replace('Employee: ','').split(" ")[0];    
      employeeObj.empId = empId;
    }
    else if(line.includes('Total Earnings')) {
      const amount = line.replace(/[^0-9.]/g,'');      
      employeeObj.TotalEarning = amount;
    } 
    else if(line.includes('Profession Tax')) {        
      const amount = line.split(" ").pop() || 0;    
      employeeObj.ProfessionalTax = amount;
    } 
    else if(line.includes('Gross Income')) {
      const amount = line.replace(/[^0-9.]/g,'');      
      employeeObj.GrossIncome = amount ||0;
    } 
    else if(line.includes('finance department immediately')) {
      employeeObj.isDone = true;   
      return callback(employeeObj);
    }
    
  });

  fileContent.on('close', function() {
    fileContent.close();      
  }); 
}

readFiles(directoryPath);

当然,代码可以进一步改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM