[Chartjs]-Converting JSON data into Chart.js array from strings

1👍

I’m not sure if that’s what you’re looking for but you could simply return an Object instead of a String. Therefore return:

{x: e.time, y: e.fitted}

instead of:

'{x:' + e.time + ', y:' + e.fitted + '}'

in your mapping.

Here is the code:

const jsonFile = {profiles:[{time:0,conc:.15,fitted:.1,non:0,prior:30,missed:2.9,wrong:2.9,mix:.15,temp:.015,gel:.03,data:4},{time:1,conc:11,fitted:10,non:5,prior:35,missed:2.35,wrong:25,mix:11,temp:1.1,gel:2.2,data:4}]};


const dataPairs = jsonFile.profiles.map(e => ({x: e.time, y: e.fitted}) )

console.log(dataPairs)

2👍

Just format them in such a way in a first call to map. Instead of using map to produce array of stings, use it to produce array of objects.

jsonFile.profiles.map(v => ({ x: v.time, y: v.fitted }) );
var jsonFile = {
  "profiles": [
    { "time": 0, "conc": 0.15, "fitted": 0.1, "non": 0, "prior": 30, "missed": 2.9, "wrong": 2.9, "mix": 0.15, "temp": 0.015, "gel": 0.03, "data": 4 },
    { "time": 1, "conc": 11, "fitted": 10, "non": 5, "prior": 35, "missed": 2.35, "wrong": 25, "mix": 11, "temp": 1.1, "gel": 2.2, "data": 4 }
]};

const d = jsonFile.profiles.map(v => ({ x: v.time, y: v.fitted }) );

console.log(d);

Leave a comment