0

I have an array of integers

element= [12,456]

I would like to transform this into

desired_output = "[12,456]" 

I tried toString but it fails. For example element.toString() outputs "12,456" which is not what I want.

It's tricky, I want to keep the [ and the ] inside the string.

7
  • 8
    JSON.stringify([12,456]) Commented Jul 12, 2018 at 10:31
  • @SudhirBastakoti please put a formal answer so I can give you points:) thanks it worked Commented Jul 12, 2018 at 10:32
  • by the way is it possible to go the other way around like move from "[12,456]" to [12,456] ? Commented Jul 12, 2018 at 10:33
  • found it: it's json.parse Commented Jul 12, 2018 at 10:34
  • @mplungjan I tried but it seems I did not know json was the key, once you sue this keyword the answer abound! I was instead getting strange splits and joins and none working. Commented Jul 12, 2018 at 10:36

2 Answers 2

2

This might work:

JSON.stringify(YourArray);
Sign up to request clarification or add additional context in comments.

Comments

1
a = [1, 2, 3]
desired = `[${a.join(',')}]`
console.log(desired) //"[1,2,3]"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.