简体   繁体   English

带有被推入对象的数组结果仍然是空的

[英]array with objects being pushed in it turns out to be empty still

I am declaring an array list_of_blogposts and then I am opening a file to pull data from it, and in it, I am declaring an object blogpost_object in which I am entering key-value pairs.我正在声明一个数组list_of_blogposts ,然后我打开一个文件以从中提取数据,并在其中声明一个 object blogpost_object ,我在其中输入键值对。 When I console log it inside the opened file code block it does console log properly, but outside it, it shows empty, like in the last line it doesn't work.当我在打开的文件代码块中进行控制台记录时,它会正确地进行控制台记录,但在它之外,它显示为空,就像在最后一行中它不起作用一样。 Can anybody mention whats the reason and solution for it?任何人都可以提及它的原因和解决方案吗? Thanks, the code is below谢谢,代码如下

const list_of_blogposts = [];

new XLSX()
  .extract("./all_blogposts.xlsx", {
    ignore_header: 2,
    include_empty_rows: false,
    sheet_name: "all_blogposts"
  })
  .on("row", function(row) {
    var blogpost_object = {};
    blogpost_object["title"] = row[2];
    list_of_blogposts.push(blogpost_object);

    console.log(list_of_blogposts);
  });

console.log(list_of_blogposts);

This is because the function(row) {... is a callback function thus it is executed asynchronously.这是因为function(row) {...是一个回调 function 因此它是异步执行的。 So the flow of execution will be like所以执行流程就像

  1. execute .on()执行.on()
  2. call the function in callback function(row) {...在回调function(row) {...
  3. execute the console.log outside the function在function之外执行console.log
  4. finally, the execution of the callback function最后,执行回调 function

Thus, the console.log you have outside the function is executed before the callback function is called so it returns an empty array.因此,您在 function 之外的console.log在调用回调 function 之前执行,因此它返回一个空数组。 The console.log within the function returns the array after the values are pushed. function 中的console.log会在推送值后返回数组。

So what you can do to solve this problem is do whatever you want to do with the array of values within the callback function.因此,解决此问题的方法是对回调 function 中的值数组执行任何操作。

const doAction = (list) => {
   //Do whatever you want
}

const list_of_blogposts = [];

new XLSX()
  .extract("./all_blogposts.xlsx", {
    ignore_header: 2,
    include_empty_rows: false,
    sheet_name: "all_blogposts"
  })
  .on("row", function(row) {
    var blogpost_object = {};
    blogpost_object["title"] = row[2];
    list_of_blogposts.push(blogpost_object);

    console.log(list_of_blogposts);
    doAction(list_of_blogposts)
  });

console.log(list_of_blogposts);

Hope it helped!!!希望有帮助!!!

You can use the .on('end' method for actions you need to perform after all the rows have been read. Similarly, you can use .on('error' for handling error scenarios.您可以将.on('end'方法用于在读取所有行后需要执行的操作。类似地,您可以使用.on('error'来处理错误场景。

The code can be代码可以

const list_of_blogposts = [];
new XLSX()
  .extract("./all_blogposts.xlsx", {
    ignore_header: 2,
    include_empty_rows: false,
    sheet_name: "all_blogposts"
  })
  .on("row", function(row) {
    var blogpost_object = {};
    blogpost_object["title"] = row[2];
    list_of_blogposts.push(blogpost_object);

    console.log(list_of_blogposts);
  }).on('end', function (err) {
    console.log(list_of_blogposts);
    your_method(); // you can perform anything you want after all rows are read
  });

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

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