0

I want to add a new property to my Object. But it adds the property in double-quotes and the object is not able to initialize well. Here is my code in the JSFiddle.

Here what I expect is like Expectation

This is how it comes Not correct

Thanks for any suggestion in advance.

var chart;
function myFunction(incoming_data) {
var lcl = JSON.parse(JSON.stringify(incoming_data));
var headertxt = lcl.title_dynamic;
console.log(headertxt);
var chartCfg = {
            zoomEnabled: true,
            axisX: {
                title: "chart updates every 2 secs",
                crosshair: {
                    enabled: true,
                    snapToDataPoint: true
                }
            },
            axisY: {
                crosshair: {
                    enabled: true,
                    snapToDataPoint: true,
                    valueFormatString: "#,##0"
                }
            },
            toolTip: {
                shared: true
            },
            legend: {
                dockInsidePlotArea: true,
                verticalAlign: "top",
                horizontalAlign: "right"
            }
        };
chart = new CanvasJS.Chart("chart-container", chartCfg);
        chart.options.title = headertxt;
console.log(chart.options);
        chart.render();
}
var json_val = {"title_dynamic":"{text:\"Header Tay\"}"};
myFunction(json_val);
  <div id="chart-container" style="height: 300px; width: 100%;">
  </div>
<div style="margin-top:16px;color:dimgrey;font-size:9px;font-family: Verdana, Arial, Helvetica, sans-serif;text-decoration:none;"></div>
    <script type="text/javascript" src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.stock.min.js"></script>

EDIT: I attached the code snippet.

Taylan Yuksel
  • 345
  • 3
  • 12
  • Please click edit, then `[<>]`snippet editor and provide a [mcve] – mplungjan Mar 24 '21 at 11:39
  • `headertxt` *is* text, as the variable name suggests. You need: `JSON.parse(headertxt)`. – VLAZ Mar 24 '21 at 11:40
  • @VLAZ I get the following error when I use as you suggessted ```Uncaught SyntaxError: Unexpected token t in JSON at position 1``` – Taylan Yuksel Mar 24 '21 at 11:51
  • Ah, right, it's *not* JSON, just a weird text version of a JS object, for some reason. The easiest immediate fix is to `eval` it but I'd *very heavily suggest*, you change whatever is generating that data to produce valid JSON. – VLAZ Mar 24 '21 at 11:53
  • @VLAZ I use the CanvasJS object tho. So, this is its object. I am passing the ```headertext``` by the controller. I will also pass the ```data``` by the controller. They have to be dynamic according to selected parameters. – Taylan Yuksel Mar 24 '21 at 12:04

1 Answers1

0

What I needed to do was so simple. In case any of the fellas have a similar issue I post my solution.

Convert a string to object

var my_obj = eval('({' + txt+ '})'); 

Convert data to an array

var my_obj = eval(var_array);

Reference questions: String to object in JS Convert form data to JavaScript object with jQuery

Cheers!

Taylan Yuksel
  • 345
  • 3
  • 12