1

I'm having array of arrays like below.

const myArray = [[3,1,1,1],[4,2,1],[5,2],[6]];

I want push an empty string 5 times to each element of this array.

Expected Output Array :

[[3,1,1,1,'','','','',''],[4,2,1,'','','','',''],[5,2,'','','','',''],[6,'','','','','']]

What is the most efficient way to do this?

My approach:

const array1 = [[3, 1, 1, 1],[4, 2, 1],[5, 2],[6]];
const appendArray = new Array(5).fill('');
const map1 = array1.map(x => x.concat(appendArray));
console.log(JSON.stringify(map1));

I'm not sure the efficiency of my approach?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Adam
  • 129
  • 6
  • 1
    This might be a better fit at codereview. In any case it is very readable, self-describing and works. If you have to do it 1000 times , there may by faster ways but they would be less readable – mplungjan Oct 05 '22 at 06:24

0 Answers0