Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于在TypeScript中显示值的Foreach数组_Typescript_Loops_Pdf - Fatal编程技术网

用于在TypeScript中显示值的Foreach数组

用于在TypeScript中显示值的Foreach数组,typescript,loops,pdf,Typescript,Loops,Pdf,我正在尝试创建一个PDF,其中的数据来自数据库。下面是如何在TypeScript中声明变量 essay = { "title": "", "author": { "fullname": "" }, "intro": "", "conclusion": "", "paragraphs": [ { "paragraph": "" } ] } 正如您在这里看到的,段落是数组类型的。因此,当触发生成PDF的按钮时,将调用下面的函数 CreatePdf(){

我正在尝试创建一个PDF,其中的数据来自数据库。下面是如何在TypeScript中声明变量

essay = {
    "title": "",
    "author": { "fullname": "" },
    "intro": "",
    "conclusion": "",
    "paragraphs": [ { "paragraph": "" } ]
}
正如您在这里看到的,段落是数组类型的。因此,当触发生成PDF的按钮时,将调用下面的函数

CreatePdf(){
    var docDefinition = {
      content: [
        { text: this.essay.title, style: 'header' },
        { text: new Date().toTimeString(), alignment: 'right' },

        { text: 'Written By : '+this.essay.author.fullname, style: 'subheader' },

        { text: this.essay.intro, style: 'story', margin: [0, 20, 0, 20] },

        // Foreach essay.paragraphs and display the value

        { text: this.essay.conclusion, style: 'story', margin: [0, 20, 0, 20] }
      ]
    }
    this.pdfObj = pdfMake.createPdf(docDefinition);
    this.pdfObj.download();
}
问题是,我将如何使用foreach来显示
内容:[]
中的所有段落值?我试图在内容中应用以下循环,但无法实现

for(let parag of this.essay.paragraphs){
  console.log(parag.paragraph);
};

您可以使用
运算符和
map()


map()
顾名思义,它使用给定的函数映射每个元素,
只需将数组展平。

工作得很有魅力!谢谢:)
CreatePdf(){
    var docDefinition = {
      content: [
        { text: this.essay.title, style: 'header' },
        { text: new Date().toTimeString(), alignment: 'right' },

        { text: 'Written By : '+this.essay.author.fullname, style: 'subheader' },

        { text: this.essay.intro, style: 'story', margin: [0, 20, 0, 20] },

        ...this.essasy.paragraphs.map( p => {
            return {text: p.paragraph, style: 'story', margin: [0, 20, 0, 20]};
        }),

        { text: this.essay.conclusion, style: 'story', margin: [0, 20, 0, 20] }

      ]
    }
    this.pdfObj = pdfMake.createPdf(docDefinition);
    this.pdfObj.download();
}