Jitendra Koyande
Jitendra Koyande

Reputation: 59

Conversion of string array to number array in javascript

I have the following string

salesdata=[[0],[0],[0.767],[1.366],[2.003],[15.128],[32.766],[57.225],[0],[0],[0],[0]];

I want to convert it to an integer array with the same format of brackets to pass as highcharts input. I tried this

var data=salesdata.split(",");

Upvotes: 1

Views: 70

Answers (3)

Waleed Arshad
Waleed Arshad

Reputation: 1099

You can use JSON.parse() to convert that string array to actual array type.

console.log(JSON.parse(salesdata))

Upvotes: 4

Muthukrishnan
Muthukrishnan

Reputation: 2197

Assuming your salesdata is of the below format:


    var salesdata='[[0],[0],[0.767],[1.366],[2.003],[15.128],[32.766],[57.225],[0],[0],[0],[0]]';

You can convert it to integer using,


    var salesdata_array = JSON.parse(salesdata);
    console.log(salesdata_array[0])

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use eval() to convert that string array to actual array type. But note that eval() is highly discouraged to use in the code.

var salesdata=`[[0],[0],[0.767],[1.366],[2.003],[15.128],[32.766],[57.225],[0],[0],[0],[0]]`;
console.log(eval(salesdata));

Upvotes: 1

Related Questions