0

In javascript I currently have: "[11,8,7,6,5,4]"

I would like the convert this into ["11", "8", "7", "6", "5", "4"]. I've tried using .split() on the string but it is not returning the desired result, along with any other attempts. Thanks

b.herring
  • 563
  • 2
  • 18
  • 1
    Does this answer your question? [Convert string with commas to array](https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array) – Nick Jun 18 '20 at 13:05
  • can you show some code of what you tried with split()? – Danimal Jun 18 '20 at 13:06

6 Answers6

5

The fastest way: (Updated)

JSON.parse("[11,8,7,6,5,4]").map(String)
demkovych
  • 7,827
  • 3
  • 19
  • 25
2

By making use of match method of string and this regex /\d+/g, will get you the expected output.

var string = "[11,8,7,6,5,4]";

var result = string.match(/\d+/g);

console.log(result);
gorak
  • 5,233
  • 1
  • 7
  • 19
0

Seems you are looking for this:

console.log(eval("[11,8,7,6,5,4]").map(String))
Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20
0
  • Parsing a string will convert it in an array.
  • Later then you can map each element of array to be a String, then you will have Array Of Strings

let array = JSON.parse("[11,8,7,6,5,4]");
let arrayOfStrings = array.map(item => `${item}`);

console.log('array', array);
console.log('arrayOfStrings', arrayOfStrings);

PS: I've used arrow functions, you might switch to basic functional syntax.

Mohit G.
  • 86
  • 7
  • 1
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 18 '20 at 18:15
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Abhishek Bhagate Jun 18 '20 at 19:15
  • Thanks, I totally agree with your thoughts and will modify my answer. – Mohit G. Jun 19 '20 at 04:56
0

You could use a for loop to convert them into strings if that's all you want:

for(num of numArray){
num = num.toString();
}
SuperColin
  • 131
  • 1
  • 9
0
var obj = JSON.parse("[11,8,7,6,5,4]");
var out=obj.map(String);
console.log(out);
  1. Parse the data with JSON.parse(), and the data becomes a JavaScript object. (https://www.w3schools.com/js/js_json_parse.asp)

  2. .map function is like apply function of python or R, which acts on each element of the incoming array using the function defined within the parentheses; i.e. The map() method creates a new array with the results of calling a function for every array element. (https://www.w3schools.com/jsref/jsref_map.asp)

  • While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 18 '20 at 18:17