0

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.

3
  • What is your end goal exactly? Why do you need to use indexOf for commas? Commented May 30, 2022 at 7:57
  • 1
    Search after join. And before joining the items, remove the space (if necessary) with f. e. map. Commented May 30, 2022 at 7:59
  • Does this answer your question? Easy way to turn JavaScript array into comma-separated list? Commented May 30, 2022 at 8:48

2 Answers 2

1
let yourString = yourArray.map(e => e.replace(/\s/g, "")).join(",")
Sign up to request clarification or add additional context in comments.

1 Comment

.trim() only remove the leftmost and rightmost space, but not the space in between words.
1
let array = [ "Car 1" , "Car 2"  ,"Car 3" , "Car 4" , "Car 5"  ];
let newString = array.map(e => e.replace(/\s/g, '')).join(",")

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

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.