Sal-laS
Sal-laS

Reputation: 11639

Typescript: concate all the elements in an string array

I have an array of string:

public scene: Array<string> = ['gittare','saxsophone','drum'];

I need to concatenate all the string in the array like 'gittare_saxsophone_drum' . Here is my solution in recursive way:

addressCreator(array:Array<string>){


      if(array.length>0)
           var fileName=array[0]+"_"+this.addressCreator(array); 
      else
          return [];
      return fileName;
  }

The none recursive approach could be:

addressCreator(array:Array<string>){

      array.splice(0,1);
      for(let e of array)
           var fileName=e+"_"+fileName; 

      return fileName;
  }

In both of these solutions, i nused var which shouldn't be used in functional programming.

So what is the best approach?

Upvotes: 0

Views: 679

Answers (1)

toskv
toskv

Reputation: 31600

You can just use the join method for that.

let x = scene.join("_");

Upvotes: 3

Related Questions