-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  "]
4
  • 1
    What's your question? What have you tried? What are you having difficulty with? Commented May 6, 2021 at 14:33
  • Do you want to repeat the array 3 times? Commented May 6, 2021 at 14:35
  • i need array repeated 5times with one space added after 1 to each of string, 2 spaces after second and so on Commented May 6, 2021 at 14:36
  • yes som shekhar Mukharjee, but with space on each string after first and 2 space after second time Commented May 6, 2021 at 14:37

1 Answer 1

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; }

Sign up to request clarification or add additional context in comments.

8 Comments

the third time string should be with 2 spaces
just add another space. please see edit.
but then it will come on all time, i mean to say that on 1repeat it should be one space and on second repeat it should be 2 spaces and so on
please add the wanted result.
["ABCDE", "BCDEF", "BACDF", "ACDLK", "ABDCDE", "CDCDE", "ABCDE ", "BCDEF ", "BACDF ", "ACDLK ", "ABDCDE ", "CDCDE ", "ABCDE ", "BCDEF ", "BACDF ", "ACDLK ", "ABDCDE ", "CDCDE "]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.