Home > OS >  How to change an array of objects to a string list with commas?
How to change an array of objects to a string list with commas?

Time:05-30

I currently have an array in angular that I would like to change the structure of. Example array

0: "Car 1" 1: "Car 2" 2: "Car 3" 3: "Car 4" 4: "Car 5" 5: "Car 6"

I would like to convert it to "Car1,Car2,Car3,Car4,Car5" so that I can use the IndexOf() function for each comma.

CodePudding user response:

let yourString = yourArray.map(e => e.replace(/\s/g, "")).join(",")

CodePudding user response:

let array = [ "Car 1" , "Car 2"  ,"Car 3" , "Car 4" , "Car 5"  ];
let newString = array.map(e => e.replace(' ', '')).join(",")

Output will be - Car1,Car2,Car3,Car4,Car5

  • Related