-2

Using array below, I need a loop to get specific result given below:

var arr = ["ABCDE", "BCDEF", "BACDF", "ACDLK", "ABDCDE", "CDCDE"];

Every time repeat while array result with one space. The results should look like this:

["ABCDE", "BCDEF", "BACDF", "ACDLK", "ABDCDE", "CDCDE", "ABCDE ", "BCDEF ", "BACDF ", "ACDLK ", "ABDCDE ", "CDCDE ", "ABCDE  ", "BCDEF  ", "BACDF  ", "ACDLK 
 ", "ABDCDE  ", "CDCDE  "]

1 Answers1

0

You could take a closure and add the suffix to the values.

const
    suffix = '*',
    getArray = (array, length) => Array
        .from(
            { length },
            (_, i) => array.map(s => s + suffix.repeat(i))
        )
        .flat(),
    result = getArray(['a', 'b', 'c'], 4);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392