1

I have a string like this: var data = "[[1, 2, 3,], [3, 2, 1]]"; I am using the well known blossom function, which expects (as input) an array of this type: [[1, 2, 3,], [3, 2, 1]], [...], [...]]

As you can see, I am trying to provide this with my: data variable, but this is currently a string, so I need to convert it to the right type. I know that I can convert a string to an array like this:

var input = "1, 2, 3";
var array = input.split(",");

Then the result of the above will be: ["1", "2", "3"]

But I need to be able to give input like this: var data = "[[1, 2, 3,], [3, 2, 1]]"; And expect this output: [[1, 2, 3,], [3, 2, 1]]; Also, notice that the values in the brackets are numbers and not string.

I also tried doing this:

var data = "[1, 2, 3,], [3, 2, 1]"; var res = data.split(",");

This gives the output: ["[1", " 2", " 3", "]", " [3", " 2", " 1]"]

Finally I also found this example: Convert string with commas to array where the solution made use of JSON.parse(...), but I did not make this work either.

halfer
  • 19,824
  • 17
  • 99
  • 186
MOR_SNOW
  • 787
  • 1
  • 5
  • 16
  • Is the trailing comma in the first array a typo ? If not it will make it harder to use `JSON.parse`. If it is then just go for it. – Denys Séguret Mar 12 '17 at 14:03
  • No, the commas are not a type :) – MOR_SNOW Mar 12 '17 at 14:20
  • If you control and trust the source of the data, you can use `Function` constructor as a substitute for `eval`. It evaluates the code just like any other script, hence the trust caveat, but by using `Function` it at least gets eval'd as though the function was created in the global scope, and it avoids performance problems. `var array = new Function("return " + data)();` –  Mar 12 '17 at 14:23

2 Answers2

4

If your array have trailing commas you can use this code:

var data = "[[1, 2, 3,], [3, 2, 1]]";
var array = JSON.parse(data.replace(/,\s*]/g, ']'));
console.log(array);

and if you have string like this:

var data = "[1, 2, 3,], [3, 2, 1]";

use

var array = JSON.parse('[' + data.replace(/,\s*]/g, ']') + ']');

Example:

var data = "[1, 2, 3,], [3, 2, 1]";
var array = JSON.parse('[' + data.replace(/,\s*]/g, ']') + ']');
console.log(array);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • This give the output: "1,2,3,3,2,1". It's as if it ignores the brackets? – MOR_SNOW Mar 12 '17 at 14:20
  • @MOR_SNOW: No, it doesn't. It gives you an array. Apparently the way you're **outputting** that array makes it look like that. – T.J. Crowder Mar 12 '17 at 14:21
  • I need it to output this: "[[1,2,3,3,2,1]]". I could add those manually of course, but maybe you know a smarter way to it, in addition to the code you provided? – MOR_SNOW Mar 12 '17 at 14:23
  • 2
    @MOR_SNOW: That isn't the output you show in the question. –  Mar 12 '17 at 14:25
  • Never mind, it's correct as you provided already. I missed something. Thanks man – MOR_SNOW Mar 12 '17 at 14:25
1

As I said in comment, this isn't JSON, as there are trailing commas.

Modifying a string which isn't in JSON to make it possible to use JSON.parse looks like a silly hack, especially when a proper parsing is easy.

An alternative would be:

var arr = str.split(/[\[\]]+,?\s*/)
    .filter(Boolean)
    .map(s=>s.split(/\D+/).filter(Boolean).map(Number));
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758