0

I'm plotting a graph using highchart but date in not properly displaying in x-axis so i need to parse the dates from the json response. I tried to manipulate the json date but not getting as expected.

This is my current json response

"linechart":[{
                "name":"Average Conversation Length (In minutes)",
                "data":[["2020,02,12",12],["2020,02,13",13]]
            },{
                 "name":"Average Number of Conversations",
                 "data":[["2020,02,12",10],["2020,02,13",21]]
            }]

This is my expected output

linechart: [{name: "Average Conversation Length (In minutes)",
            data: [[Date.UTC(2020, 02, 12), 12],[Date.UTC(2020, 02, 13), 13]]},
            {name: "Average Number of Conversations",
            data: [[Date.UTC(2020, 02, 12), 10],[Date.UTC(2020, 02, 13), 21]]}],

This is what i tried

    var arr1 = [];
var data = chartdata.linechart
for (var i = 0; i < data.length; i++){
    var obj = data[i];
    for (var key in obj){
      var value = obj[key];
      for (var value2 in value){
        var values = value[value2];
        arr1.push(Date.parse(values));
      }
    }
  }

3 Answers 3

1
var data = chartdata.linechart;
for (var i = 0; i < data.length; i++) {
  var obj = data[i];
  obj.data.forEach(d => {
    d[0] = Date.parse(d[0]);
  });
}

console.log(data);
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply map over the elements:

let linechart = [
  {
    "name":"Average Conversation Length (In minutes)",
    "data":[["2020,02,12",12],["2020,02,13",13]]
},{
    "name":"Average Number of Conversations",
    "data":[["2020,02,12",10],["2020,02,13",21]]
}]

const res = linechart.map(o => {
    return {name: o.name, data: o.data.map( ([d,n]) => {
        return [Date.parse(d), n]
    })}
})

console.log(res)

Comments

0

You can also try this approach:

let jsonData = [{
  "name": "Average Conversation Length (In minutes)",
  "data": [
    ["2020,02,12", 12],
    ["2020,02,13", 13]
  ]
}, {
  "name": "Average Number of Conversations",
  "data": [
    ["2020,02,12", 10],
    ["2020,02,13", 21]
  ]
}];

jsonData.forEach(item => {
    item.data.forEach(data => {
        let date = data[0].split(',');
        
        data[0] = Date.UTC(date[0], date[1], date[2])
    })
})

https://jsfiddle.net/BlackLabel/ucx6ojL2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.