Chartjs-How to target only X or Y value in jsonObj array

3👍

You can use the following code to separate your data into X and Y arrays:

let data = jsonObj.result.data;
let dataX = data.map(data => data.x);
let dataY = data.map(data => data.y);

Demo:

const json = `
{
    "action": "data_link_day",
    "result": {
        "url_ending": "0",
        "data": [{
            "x": "2018-03-12",
            "y": 3
        }, {
            "x": "2018-03-16",
            "y": 5
        }]
    }
}`;

const input = JSON.parse(json);

let data = input.result.data;
let dataX = data.map(data => data.x);
let dataY = data.map(data => data.y);

console.log(dataX);
console.log(dataY);

Leave a comment