最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use array.map with a 2-dimensional array - Stack Overflow

programmeradmin4浏览0评论

I need to loop through an array, but the statement is located between brackets [ ]

It is however not allowed to place a for-statement between brackets and will therefore result in an SyntaxError: missing 'of' after for

searching the internet, I found out that array.map() could be a solution. I could however not find a example of an array.map on a multidimensional array, and can't get my code to work.

The example below creates a table and exports it into a pdf file. This works correctly, but works only for a static table:

var docDefinition = {
content: [
{
  table: {
    body: [
      [ 'First', 'Second', 'Third', 'The last one' ],
      [ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],
      [ 'Val 1', 'Val 2', 'Val 3', 'Val 4' ]
    ]
  }
}
]};

JSFiddle

I need the code to work for a dynamic table. I'm trying to create a table from an array, can't get it to work properly. This is what I have so far:

function html2Pdf(){
var arr = [[ 'First', 'Second', 'Third', 'The last one' ],[ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],[ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ]];    
var docDefinition = {
content: [
{
  table: {
    body: [
     arr.map(function(item){
        return item
     })    
    ]
  }
}
]
};
pdfMake.createPdf(docDefinition).open();
}

JSFiddle

I need to loop through an array, but the statement is located between brackets [ ]

It is however not allowed to place a for-statement between brackets and will therefore result in an SyntaxError: missing 'of' after for

searching the internet, I found out that array.map() could be a solution. I could however not find a example of an array.map on a multidimensional array, and can't get my code to work.

The example below creates a table and exports it into a pdf file. This works correctly, but works only for a static table:

var docDefinition = {
content: [
{
  table: {
    body: [
      [ 'First', 'Second', 'Third', 'The last one' ],
      [ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],
      [ 'Val 1', 'Val 2', 'Val 3', 'Val 4' ]
    ]
  }
}
]};

JSFiddle

I need the code to work for a dynamic table. I'm trying to create a table from an array, can't get it to work properly. This is what I have so far:

function html2Pdf(){
var arr = [[ 'First', 'Second', 'Third', 'The last one' ],[ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],[ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ]];    
var docDefinition = {
content: [
{
  table: {
    body: [
     arr.map(function(item){
        return item
     })    
    ]
  }
}
]
};
pdfMake.createPdf(docDefinition).open();
}

JSFiddle

Share Improve this question asked Aug 13, 2017 at 11:12 DummyDummy 3731 gold badge3 silver badges11 bronze badges 2
  • 2 Why don't you just have body: arr? – Evan Trimboli Commented Aug 13, 2017 at 11:16
  • Also click the <> and post the code here instead of JSFiddle in a minimal reproducible example – mplungjan Commented Aug 13, 2017 at 11:18
Add a comment  | 

1 Answer 1

Reset to default 15

As commented before it's pretty okay to inject the given array into resulting structure directly.

table: {
    body: arr
  }

Regarding your question on how to use array map on two-dimensional arrays: you can nest it quite as you would do with for-loops or similar.

body: arr.map( function( row ) {
    return row.map( function( cell ) { 
        return foo( cell ); 
    } );
} )
发布评论

评论列表(0)

  1. 暂无评论