Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to access array inside object without keys

I have a JSON data which look like this:

 {
  "chart":[
     [
        1627776011000,
        28
     ],
     [
        1627776371000,
        38
     ],
     [
        1627776731000,
        48
     ],
     ...
    ]
}

I want to use this data in Chart.js where 1627776011000 ... are x axis values and 28 ... are y axis values.

I know I can do it with an array push

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const myArr = JSON.parse(dataJSON);

const data = []; 
const labels = [];

myArr.chart.forEach((item) => {
    labels.push(item[0]);
    data.push(item[1]);
});

but could there be a better way, for example with a map?

>Solution :

You can do it as two calls to map():

const labels = myArr.chart.map(el => el[0]);
const data = myArr.chart.map(el => el[1]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading