简体   繁体   English

如何将多个数组的结构更改为数组对象Node.js?

[英]How I can change structure of multiple arrays to array objects Node.js?

(Variable file contains array of URL .json files). (变量文件包含URL .json文件数组)。 One file in subdirectories contains: 子目录中的一个文件包含:

[ {"name": "John"} ]

Then I get common file get.json who contains all files from subdirectories in structure (yeap, I know it's not a valid JSON): 然后,我得到通用文件get.json,该文件包含结构子目录中的所有文件(是的,我知道这不是有效的JSON):

[ {"name": "John"} ]
[ {"name": "Sergei"} ]

I want get this file in this structure: 我想要此文件的结构如下:

[ {name: "John"}, {name: "Sergei"} ]

My code 我的密码

recursive(`${dirPath}`, ['delete.json', 'put.json'], function (err, file) {

  const write = fs.createWriteStream(`${dirPath}/get.json`);

  file.forEach(item => {
    fs.createReadStream(item).pipe(write);
  })

  write.on('finish', () => {
    fs.createReadStream(`${dirPath}/get.json`).pipe(res);
  })

});

One day from my life left with this task, done! 离我生命中的这一天完成了!

recursive(`${dirPath}`, ['delete.json', 'put.json'], function (err, file) {

  const write = fs.createWriteStream(`${dirPath}/get.json`);

  const usersArr = [];

  let readStream;

  file.forEach(item => {
    readStream = fs.createReadStream(item);
    readStream.on("data", (data) => {
      JSON.parse(data).map(item => usersArr.push(item));
    });
  })

  readStream.on("end", (data) => {
    write.write(JSON.stringify(usersArr, null, " "), () => {
      console.log('Completed');
    });

    fs.createReadStream(`${dirPath}/get.json`).pipe(res)
  });

});
const names = data.split('\n')
  .map(nameArrayString => (JSON.parse(nameArrayString)[0]))

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

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