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","!"]
8
  • 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. Commented Aug 19, 2018 at 0:12
  • 3
    seems like [...text.repeat(n)] Commented Aug 19, 2018 at 0: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. Commented Aug 19, 2018 at 0:41
  • 1
    No, @-mentions in quesions don't notify the user, I just browse new questions frequently :) Commented Aug 19, 2018 at 0:42
  • 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. Commented Aug 19, 2018 at 3:42

3 Answers 3

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(''));

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

3 Comments

Thanks for your answer, however, I need to put the same string in n times, not n strings to be put in.
@JayJenkins I've updated my answer based on your comment. Check it now, is this what you want?
Thanks @V. Sambor, it's already been answered in a comment above. The same as yours almost, though a little more elegant.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.