创建对象的对象

问题描述:

我想在JS中创建对象的对象,但我认为我有一些异步执行的问题。创建对象的对象

这里是我的代码:

// Extract data (episode number, characters firstname's) from .docx to JSON 
 

 
var mammoth = require("mammoth") 
 

 
function docx2Object (filepath) { 
 
    
 
    return mammoth.extractRawText({path: filepath}) 
 
    .then(function (res) { 
 
     return res.value; 
 
    }) 
 
    .then(function (res) { 
 
     let arr = {} 
 
     arr["number"] = "" + res.match(/(?!Episode #)\d+/) 
 
     arr["names"] = [...new Set(res.match(/^[^:\r\n]+(?=:)/gm))] 
 
     return arr 
 
    }) 
 
} 
 

 
function files2JSON (arrayScripts) { 
 
    
 
    let arr = {} 
 
    let i = 0 
 
    arrayScripts.forEach(function(element) { 
 
     docx2Object(element).then(function (res) { 
 
      arr[i++] = res 
 
      console.log(JSON.stringify(arr, null, '\t')) 
 
     }) 
 
    }) 
 
    return JSON.stringify(arr, null, '\t') 
 
} 
 

 
let arrayScripts = ["doc1.docx", "doc2.docx"] 
 
console.log(files2JSON(arrayScripts))

这里是输出:

{} 

{ 
    "0": { 
     "number": "10600", 
     "names": [ 
      "Hilary", 
      "Jean-Claude", 
      "Devon", 
      "Jean Claude", 
      "Cane", 
      "Lily", 
      "Jack", 
      "Phyllis", 
      "Victor", 
      "Nikki", 
      "Neil", 
      "Paul", 
      "Dr. Barrett", 
      "Christine", 
      "Kelly" 
     ] 
    } 
} 
{ 
    "0": { 
     "number": "10600", 
     "names": [ 
      "Hilary", 
      "Jean-Claude", 
      "Devon", 
      "Jean Claude", 
      "Cane", 
      "Lily", 
      "Jack", 
      "Phyllis", 
      "Victor", 
      "Nikki", 
      "Neil", 
      "Paul", 
      "Dr. Barrett", 
      "Christine", 
      "Kelly" 
     ] 
    }, 
    "1": { 
     "number": "10601", 
     "names": [ 
      "Hilary", 
      "Devon", 
      "Jean+Claude", 
      "Jean + Claude", 
      "Jean/Claude", 
      "Jean/Claude", 
      "Cane", 
      "Lily", 
      "Jack", 
      "Phyllis", 
      "Victor", 
      "Nikki", 
      "Neil", 
      "Paul", 
      "Dr. Barrett", 
      "Christine", 
      "Kelly" 
     ] 
    } 
} 

我的数组是空的,我的索引不match.Can谁能帮助?

+0

你试过调试它,把一堆控制台日志放到你的函数中。并查看哪些执行,哪些不执行和您的对象发生了什么。并告诉我,如果我错了,但你不能有一个以上的回报,因为第一个将取消一切波纹 – noitse

我觉得你的问题是与

arrayScripts.forEach(function(element) { 
    docx2Object(element).then(function (res) { 
     arr[i++] = res 
     console.log(JSON.stringify(arr, null, '\t')) 
    }) 
}) 

docx2Object呼叫在forEach的异步含义迭代将返回,并立即执行。这意味着files2JSON可能会在所有处理完成之前执行。

最有可能的是将每个承诺存储到一个数组中,然后使用Promise.all()一次性解决它们,并从Promise.all()返回承诺并等待它。总体来说这个结构会看起来像

function asyncTask1(task) { 
    return getPromise(task); 
} 

function asyncTask2() { 
    var tasks = ["eat", "sleep"]; 
    var promises = tasks.map(function(task) { 
    return asyncTask1(task); 
    }); 
    return Promise.all(promises); 
} 

asyncTask2().then(function(response) { 
    console.log("I ate and slept."); 
}); 

一件事调试异步代码只是抛出一大堆console.log S和看什么时候被执行时,我亲自做的。它确实有助于看到执行顺序。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

+0

非常感谢,它的工作! – amerej

这将有助于让你的output.As你使用全局错误的增量次数也含有错误代码mammoth.extractRawText()方法。

// Extract data (episode number, characters firstname's) from .docx to JSON 
var mammoth = require("mammoth"); 

function docx2Object (filepath) { 

    return mammoth.extractRawText({path: filepath}) 
     .then(function (res) { 
     return res.value; 
     }).then(function (res) { 
     var arr = {}; 
     arr["number"] = "" + res.match(/(?!Episode #)\d+/); 
     //arr["names"] = [...new Set(res.match(/^[^:\r\n]+(?=:)/gm))]; 
     return arr 
     }); 

} 

function files2JSON (arrayScripts) { 
    var arr = {}; 

    arrayScripts.forEach(function(element,index) { 
    docx2Object(element).then(function (res) { 
     arr[index++] = res; 
     console.log(res,index) 
     console.log(JSON.stringify(arr, null, '\t')); 
    }) 
    }); 
    return JSON.stringify(arr, null, '\t'); 
} 

var arrayScripts = ["doc1.docx", "doc2.docx"]; 
console.log(files2JSON(arrayScripts));