1

I'm doing an exercise (self-study) in which I must have an array that has a string being inserted into it n times.

I have this

var splitTxt = [];

for(i=0 ; i<n ; i++)
  {
    splitTxt += text.split('');
  }

text being the string given in the function. I've looked around but I only ever see advice on how to add characters and other strings etc. to the ends of arrays.

Adding split normally produces the desired result, however, when looping it like this, i get a comma in every index in the array. What is happening here, and how do I do it properly?

I can do this:

for(i=0 ; i<n ; i++)
  {
    splitTxt.push(text.split(''));
  }

But that produces a nested array, not desired.

I can also do this:

var secondChar = [].concat(...Array(n).fill(text.split('')));

But, again, nested array. I like this one though, using the array constructor to mess with it, very clever. An answer given by @CertainPerformance here

EDIT: Sorry, I wasn't clear enough. I'd like to split it into the array multiple times like so:

var text = "hello there!";
n = 3;

desired result: ["h","e","l","l","o"," ","t","h","e","r","e","!","h","e","l","l","o"," ","t","h","e","r","e","!","h","e","l","l","o"," ","t","h","e","r","e","!"]
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jay Jenkins
  • 413
  • 1
  • 5
  • 15
  • 1
    Can you give an example of the input `text`, `n`, and desired output? Your `splitTxt += text.split('');` is using the addition operator on the `splitTxt` *array*, which probably isn't what you want, because it results in `splitTxt` being reassigned to a *string* rather than an array. – CertainPerformance Aug 19 '18 at 00:12
  • 3
    seems like `[...text.repeat(n)]` – Slai Aug 19 '18 at 00:39
  • @CertainPerformance Thanks for your input, I hope I didn't rudely summon you with my mention in the question :) I'm not familiar with stackexchange's syntax. I appended my question with your request. – Jay Jenkins Aug 19 '18 at 00:41
  • 1
    No, @-mentions in quesions don't notify the user, I just browse new questions frequently :) – CertainPerformance Aug 19 '18 at 00:42
  • @CertainPerformance Pleasantly surprised I hope :) – Jay Jenkins Aug 19 '18 at 00:46
  • @Slai your answer gave me the desired result, thanks for your input everyone. If anyone can provide an answer they think is less resource hungry or whatever, feel free :) – Jay Jenkins Aug 19 '18 at 00:47
  • Possible duplicate of [String.ToCharArray() equivalent on JavaScript?](https://stackoverflow.com/questions/8761627/string-tochararray-equivalent-on-javascript) – Erik Philips Aug 19 '18 at 01:41
  • 1
    @ErikPhilips while asking to use some of the same methods, this is a unique question because of the iteration of 3x and pushing those into a new array. – tremor Aug 19 '18 at 03:42

3 Answers3

2

After seeing you edit, the simplest way to achieve what you want can be done in a single line:

console.log('hello there!'.repeat(3).split(''));
V. Sambor
  • 12,361
  • 6
  • 46
  • 65
1

Wanted to see if I could do this without using repeat and split, and here is what I came up with.

function mapx(text, x) {
    var myArray = [];
    for (var y = 0; y < x; y++) { 
        myArray = myArray.concat(Array.prototype.map.call(text, i => i));
    }
    return myArray;
}

var result = mapx('hello there!', 3);

console.log(result);

array.map and array.concat are pretty well supported in browsers. You could use .split instead .map - in fact I believe split benchmarks faster than map in small arrays. When objects get larger map has an advantage.

tremor
  • 3,068
  • 19
  • 37
0

Based on your example, just repeat the text n times and then split it:

function splitText(text, n) {
  return text.repeat(n).split('');
}

var result = splitText('hello there!', 3);
console.log(result);

Keep in mind that String.prototype.repeat isn't supported by older browsers, but it can be easily polyfilled.

cyr_x
  • 13,987
  • 2
  • 32
  • 46