eclipseIzHere
eclipseIzHere

Reputation: 83

How to find every comma and replace it with >

so i have a array

var arr=[1,2,3,4,5];

and i want the end result to be like this

var str="1>2>3>4>5>";

How do i do that?

I tried toString method and received this

1,2,3,4,5

but i need to replace every comma with > thanks in advance.

Upvotes: 0

Views: 59

Answers (2)

Ori Drori
Ori Drori

Reputation: 191976

Concat an empty string to the array, and then join('>') it:

var arr=[1,2,3,4,5];

var result = arr.concat(['']).join('>');

console.log(result);

Upvotes: 1

David
David

Reputation: 7105

What you're looking for is Array.join().

var arr=[1,2,3,4,5];
console.log(arr.join('>')); // prints "1>2>3>4>5"

If you want the trailing > you'll just want to append > to your string, like so: arr.join('>') + ">"

Upvotes: 3

Related Questions