Ruby => JS: Fill a New Array

Tech: javascript, ruby

Challenge: in a React app, I have to format some data coming from an API… I want to fill out the API data array to a certain length with empty strings. Coming from a Ruby background, I have grown very used to Ruby shortcuts. How do I tackle in JS?

Code:

incoming API data = ['string', 'string']

application needs an array with length 5… likeĀ ['string', 'string', "", "", ""]

 

In Ruby, I calculate the difference in lengths and create a new array with empty strings, contained within the .new args:

Array.new(5 - apiArray.length, "")

 

Javascript is just slightly different. Just need to combine new Array with an ES6 prototype!

new Array(5 - apiArray.length).fill("")

 

 

Reference:

https://ruby-doc.org/core-1.9.3/Array.html#method-i-fill

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill