2

I have a Json format like:

 var d =  "1,3,2,4"

How do I convert it into

 var d =   [3,5,3,6]

I tried this:

success: function (Response) {
    debugger;
    var du = (Response.d);
    var final_string = '[' + du + ']'
    // final_string = [1, 3, 2, 4];
    console.log(final_string);

But this is not working, I want to final_string value as final_string = [1, 3, 2, 4];

actually i am trying to making a graph by this data

JvaScript

<script>
        $(document).ready(function(){
            debugger;
            $.ajax({
                type: "Post",
                url: "Default.aspx/getdata",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (Response) {
                    debugger;
                    var d = Response.d.toString();
                    var final_string = '[' + d + ']'
                    console.log(final_string);
                    //final_string = [1,3,2,4];
         var options = {
             chart: {
                 height: 250,
                 width:500,
                 type: 'line',
             },
             series: [{
                 name: ' ',
                 type: 'column',
                 data: final_string
             }, {
                 name: '',
                 type: 'line',
                 data: final_string
             }],
             stroke: {

                 width: [0, 4]
             },
             title: {
                 text: 'Total Count'
             },
             labels: ['Birthady', 'Anniversary', 'Special', 'Total'],
             xaxis: {
                 type: 'text'
             },
             yaxis: [{
                 title: {
                     text: 'Count Blog',
                 },
             }, {
                 opposite: true,
                 title: {
                     text: ''
                 }
             }]
         }
         debugger;
         var chart = new ApexCharts(
             document.querySelector("#chart"),
                      options

                    );

                    chart.render();

                },
                error: function (result) {
                }
            });
        });
    </script>

here the series data format is [1,3,2,4] and when i am passing data = [1,3,2,4] in series data graph is display in correct format and when i am passing final_string in series data graph is not display in correct format what is the main issue in this format final_string can any one help me ?

aparna rai
  • 823
  • 10
  • 24

2 Answers2

2

Why not use split(",") function. Since you're using the array on a chart you can use map(Number) to convert each item of the array to a Number type instead of a String.

var d =  "1,3,2,4"
var res = d.split(",").map(Number);

console.log(res)

I based my response on the example in: ApexCharts

You need an array of Numbers like:

[2.3, 3.1, 4.0, 10.1, 4.0, 3.6, 3.2, 2.3, 1.4, 0.8, 0.5, 0.2]
César Ferreira
  • 681
  • 1
  • 5
  • 12
0
success: function (Response) {
    debugger;
    var d = Response.toString();
    var final_string = '[' + d + ']'
    console.log(final_string);
}
Suhas Bachhav
  • 403
  • 1
  • 7
  • 28