How do I transform an array that is in a string for javascript array?

Asked

Viewed 300 times

9

I have a string representing an array as follows:

var = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']";

Wish I could transform this array that is in the string into an Array to perform manipulations.

var = ['aaaaa', 'bbbbb', 'ccccc', 'ddddd'];

Thanks in advance.

6 answers

7

That string you have looks like a JSON, a representation on String of an array, so you can use JSON.parse() to transfer it to an array.

There is however a problem... ' is not valid as a string separator in JSON, so you cannot use

var arr = JSON.parse("['aaaaa', 'bbbbb', 'ccccc', 'ddddd']");

but yes:

var string = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']".replace(/'/g, '\"');
var arr = JSON.parse(string);
console.log(arr);

  • 1

    I believe that do var string = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']".replace(/\'/g, "\"") make it a little simpler for the treatment of string.

  • @Andersoncarloswoss this way is also good. I have idea that I started to use split/join after tests where I saw it was faster. But in this case it is probably irrelevant. +1

  • 1

    @Andersoncarloswoss changed, your suggestion made me go for a quick test (https://jsfiddle.net/3L5etu88/) and in fact replace it’s much better.

5

Another option is to use Array.prototype.map()

var a = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']";
var array = a.replace(/['\[\] ]/g,'').split(',').map(String);
console.log(array);

If you want to remove the whitespace at the beginning of the string

var a = "['Carro Azul', 'Carro Vermelho', 'ccccc', 'ddddd']";
var array = a.replace(/['\[\]]/g,'').split(',').map(function (str) {
   return str.trim();
});
console.log(array);

5

Just one more alternative:

var array = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']";
array = new Function("return " + array)(); // cria uma função retornando o array e a executa

console.log(array);

So whatever the content of array, the parse will be made.

  • 1

    This in practice is the same as var arr = eval("['aaaaa', 'bbbbb', 'ccccc', 'ddddd']");

  • @Sergio, it’s true, but I particularly prefer Function.

2

You can use JSON.parse to solve this, but in the format it is will not work directly, the values must be enclosed with double quotes.

In that case then you could do it this way:

//Essa parte converte as aspas simples para aspas duplas
var str = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']";
str     = str.replace(/\'/g,'"');

//Essa parte converte a string em um objeto
var arr = JSON.parse( str );

1

var data = "['aaaaa', 'bbbbb', 'ccccc', 'ddddd']";
var array = data.replace("[","").replace("]","").split(",")
  • This way the return will be array = ['\'aaaaa\'', ' \'bbbbb\'', ' \'ccccc\'', ' \'ddddd\''], where the string have blank spaces and characters ', what is not desired.

0

You can use JSON.parse() , but by default the json string values use " instead of ', this would convert the string to "

var arr = JSON.parse('["aaaaa", "bbbbb", "ccccc", "ddddd"]');
  • 2

    You changed the right AP initial string?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.