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
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
The fastest way: (Updated)
JSON.parse("[11,8,7,6,5,4]").map(String)
.map(String)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.
var obj = JSON.parse("[11,8,7,6,5,4]");
var out=obj.map(String);
console.log(out);
Parse the data with JSON.parse(), and the data becomes a JavaScript object. (https://www.w3schools.com/js/js_json_parse.asp)
.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)