So I'm working with an old codebase that uses ES5 JavaScript, this means I cannot spread arrays
var listOfItems = [ ['item1', 'test', '1'], ['item2', 'test2', '2'] ];
var docDefinition =
{
style: 'piecesTable',
table: {
widths: ['*', '*', '*'],
body: [
[
{text: 'Reference', style: 'tableHeader'},
{text: 'Alias', style: 'tableHeader'},
{text: 'Type', style: 'tableHeader'},
],
...listOfItems
]
}
};
How can I spread listOfItems without using spread syntax as seen above ...listOfItems?
The listOfItems should be spread out to two separate arrays so essentially the result should be:
var docDefinition =
{
style: 'piecesTable',
table: {
widths: ['*', '*', '*'],
body: [
[
{text: 'Reference', style: 'tableHeader'},
{text: 'Alias', style: 'tableHeader'},
{text: 'Type', style: 'tableHeader'},
],
['item1', 'test', '1'],
['item2', 'test2', '2']
]
}
};
docDefinitiondefinition, you do something likelistOfItems.forEach((item) => {docDefinition.table.body.push(item)}?concat()that everyone is suggesting works as well, and may look a little cleaner. Good luck!forEachis es5, but the arrow function is not. So it would belistOfItems.forEach(function (item) { docDefinition.table.body.push(item)})