Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
JavaScript - Converting string-array to an array of strings

I have the following input which I receive:

const myString = "['one', 'two']";

When I run the following command:

console.log(typeof myString);

I get string

This makes sense, as this is a string input. The input type is out of my control, I must receive this as a typeof string.

However, I would like to convert this string, into a formal array of strings.

For example:

const myArray = ['one', 'two'];

So, when I run:

console.log(typeof myArray);

I get object.

I have tried JSON.parse() and JSON.stringify but to no luck.

The basics of my question are how do I (in JavaScript) convert a string array, into an array of strings?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

JSON.parse would work, if the JSON wasn't invalid. As mentioned in the comments, the string would have to be formatted as '["1", "2"]'.

If you do not have control over the formatting, you can parse it manually: if the strings don't contain quotes, you can use #replaceAll("'", '"').

If you have edge cases you need to cover, json5 may be able to help: JSON5.parse(str) once you have the script loaded via NPM or unpkg

about 4 years ago · Juan Pablo Isaza Denunciar

0

JSON only recognizes the usage of " ". To parse it you need to use double quotes instead of single quotes.

const myString = "['one', 'two']"; 
// change to
const myString = '["one", "two"]';

Heres an example:

// Not working
const Str = "['one', 'two']";

console.log(Str); // ['one', 'two']
console.log(typeof Str); // string

JSON.parse(Str); // SyntaxError: Unexpected token ' in JSON at position 1

// Working example:
const myString = '["one", "two"]';

console.log(myString); // ["one", "two"]
console.log(typeof myString); // string

const myArray = JSON.parse(myString);

console.log(myArray); // [ 'one', 'two' ]
console.log(typeof myArray); // object

In the case that you need to change ' to " just use replace.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const myArray = ['one', 'two'];
let str = myArray.toString();
console.log(str);//one,two
console.log(typeof str)//string
let srr = str.split(",");
console.log(srr);//[ 'one', 'two' ]
console.log(typeof srr)//object same as that of myArray

The above variable myArray is holding an array . In order to convert the array into string there is an inbuilt method in myArray.toString(). Now to convert array to string we will use split method as shown above.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda