-1

*****No JQUERY*****

I have a string passed into my Javascript that looks like below. I want to convert it into an array.

I have

{"test":"1,180,35"}

I want

an array where index 0 = 1, index 1 = 180, index 2 = 35.

How would I achieve this?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Birdman333
  • 73
  • 2
  • 9

4 Answers4

1

Parse the string, pull out the property value for property test, split it on ,.

var input = '{"test":"1,180,35"}'
var jsObj = JSON.parse(input);
var arr = jsObj.test.split(",");
console.log(arr);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

use JSON.parse() to convert a string into a json object.

But, you are looking to parse a series of numbers into an array, so what you really want is split(",")

mankowitz
  • 1,864
  • 1
  • 14
  • 32
  • "json object" is a misnomer. json is a string format and a javascript object is the result of parsing. So you meant the latter. – Jamiec Oct 11 '19 at 15:22
-1

Use the JSON object

let arr = JSON.parse('{"test":"1,180,35"}').test.split(',');
Luke Snowden
  • 4,056
  • 2
  • 37
  • 70
-1

For example:

var yourData = `{"test":"1,180,35"}`
JSON.parse(yourData).split(',')
Bear
  • 1,017
  • 1
  • 10
  • 23