Convert a list `[1, 2, 3]` to a string '{1, 2, 3}' in Javascript -
i need convert lists [1, 3, 2]
or [1,3]
or [1,4,5]
strings of form '{1, 3, 2}'
, '{1, 3}'
'{1, 4, 5}'
.
i can think of couple of ways this, not elegant one. wondering how solve this. there elegant solution possible?
you can this.
var result = '{'+ input[0]; for(var index = 1; index < input.length; index++) { result = result + ' '+ input[index]; } result = result + '}';
or
var result = '{' + input.join(', ')+ '}';
remember give space after ','.
Comments
Post a Comment