Javascript Spread Operator Alternative

Eric Bergman

So I'm working with an old codebase that uses javascript es5, this means I cannot use the spread operator

          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 the spread operator 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']
              ]
            }
          };
charlietfl

You can use concat() to merge into your body array

     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'},
            ],
            
          ].concat(listOfItems)
        }
      };
      
      console.log(docDefinition)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

issue with spread operator in javascript

Javascript spread operator in Clojurescript?

spread operator inside for of loop javascript

Spread Operator equivalent in IE - Javascript

Spread operator not working properly in javascript

How to use spread operator in javascript?

Prototypal inheritance in JavaScript with spread operator

Javascript Spread operator confusing behavior

Express not working with spread operator in JavaScript

Javascript Spread Operator used with return

JavaScript spread operator with class instances

spread operator in javascript with key in array

javascript spread operator decision making

What is the reason for not spread (es 6 spread operator) javascript Error object

How to use spread operator on nested javascript objects?

Javascript spread operator for depth-first search

Javascript: Merge two objects with spread operator

Using spread operator multiple times in javascript?

JavaScript deep spread operator on deep arrays

Webpack is not building because of javascript spread operator

Javascript ES6 spread operator on undefined

What is the role of the spread operator in this javascript code?

How to merge with nested key in javascript spread operator?

What is the time complexity of object spread operator in Javascript?

javascript spread operator for object is not working? Is this expected behavior?

JavaScript - Object.assign to Spread operator

JavaScript | Spread operator update nested value

Kotlin: Spread operator on calling JavaScript method

JavaScript array mutating split(" ") vs spread operator

TOP Ranking

HotTag

Archive