Search code examples
node.jsautomationfs

I want contents of file in array index , please find example below


  // fs.readFile(file, [encoding], [callback]);
  // converting file and storing it in an array
  var fileRefer = fs.readFileSync('LoginID\\Creds.txt').toString().split("\n");
  for (f in fileRefer) {
    let i = fileRefer[f];
    const [, , , , id, , pw,] = i.split(" "); //pipe sep. |
    // console.log(id,pw);
    let username = id;
    let password = pw;

Example- Date: 2021-11-08 17:01:33|LoginID: pvgA1248|Password: Root@123 So , what i want is till date it should be 0th index , till loginID 1st and so on.. Please help me to achieve the same..


Solution

  • I am not sure if I get you right but you want to split all values per line in a array?

    like this:

    let str = "Date: 2021-11-08 17:01:33|LoginID: pvgA1248|Password: Root@123";
    
    let result = str.split('|').reduce((a,v) => {
      v.split(': ').forEach(s=>a.push(s));
      return a;
    },[]);
    
    console.log(result);