简体   繁体   English

function Javascript 向数组中添加数据并访问数组内部的数据

[英]Adding data to an array and accessing the data inside the array outside the function Javascript

I'm new to Javascript and I'm not understanding how to persist data in an array.我是 Javascript 的新手,我不了解如何将数据保存在数组中。 In my function I'm reading a file line by line and saving it in an array.在我的 function 中,我逐行读取文件并将其保存在数组中。 I figured that because the array is declared outside the function that data would remain in it, but that's not what happens.我认为因为数组是在 function 之外声明的,所以数据会保留在其中,但事实并非如此。 I would like to understand how to do when printing the values of the array outside the function that data still remain.我想了解在 function 之外打印数组值时该怎么做,数据仍然存在。

My code:我的代码:

const fs = require('fs');
const readline = require('readline');
  
var array = [];


async function processLineByLine() {
  const fileStream = fs.createReadStream('data.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity,
  });

  for await (const line of rl) {
    
      array.push(line);
    }
 

  //console.log(array.toString());
}

processLineByLine();
console.log(array.toString());

The expected output would be to have the data inside the array:预期的 output 将包含数组中的数据:

288355555123888,335333555584333,223343555124001,002111555874555,111188555654777,111333555123333

Because processLineByLine is an async function the console.log runs before the async has populated the array.因为processLineByLine是一个异步 function console.log在异步填充数组之前运行。

You need to log after the async function has completed, as shown below: async function 完成后需要登录,如下图:

processLineByLine().then(() => console.log(array.toString()));

Also, you don't need to declare the array outside the function, you can move it inside the function and return it from there and that would passed as an argument to the .then callback, as shown below:此外,您不需要在 function 之外声明array ,您可以将它移动到 function 内部并从那里返回它,这将作为参数传递给.then回调,如下所示:

const fs = require("fs");
const readline = require("readline");

async function processLineByLine() {
  const array = [];
  const fileStream = fs.createReadStream("data.txt");

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity,
  });

  for await (const line of rl) {
    array.push(line);
  }

  return array;
}

processLineByLine().then((array) => console.log(array.toString()));

You wont get since it's async call.你不会得到,因为它是异步调用。 try below snippet试试下面的片段

const fs = require('fs');
const readline = require('readline');

var array = [];

async function processLineByLine() {
    const fileStream = fs.createReadStream('data.txt');
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity,
    });

    for await (const line of rl) {  
        array.push(line);
    }
}

processLineByLine().then(()=>{
    console.log(array);
});

if each line of data.txt is like 288355555123888, 335333555584333, 223343555124001,... and want to extract those numbers spit each line and then add it to array.如果 data.txt 的每一行都像 288355555123888、335333555584333、223343555124001,... 并且想要提取这些数字吐出每一行,然后将其添加到数组中。

for await (const line of rl) {  
    array=[...array, ...line.split(',')];
}

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

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