I am trying to plot a chart using chart.js library.I am currently trying to plot the data of json file.
This is the code by which i am able to grab the values of sector and country properties.
const xs = [];
const ys = [];
const api_url = 'jsondata.json';
async function getData() {
const response = await fetch(api_url);
const data = await response.json();
const sector = data.map(prop => prop.sector);
xs.push(sector);
console.log(sector);
const country = data.map(prop => prop.country);
xs.push(country);
console.log(country);
}
getData();
My JSON file looks like this:
[{
"end_year": "",
"intensity": 6,
"sector": "Energy",
"published": "January, 09 2017 00:00:00",
"country": "United States of America",
},
{
"end_year": "",
"intensity": 6,
"sector": "Energy",
"published": "January, 09 2017 00:00:00",
"country": "United States of America",
}
]
I am trying to grab the values of these properties and use them as labels for x-axis and y-axis respectively.
If I understood you correctly, that is your answer
let json = [{ "end_year": "", "intensity": 6, "sector": "Energy", "published": "January, 09 2017 00:00:00", "country": "United States of America", }, { "end_year": "", "intensity": 6, "sector": "Two", "published": "January, 09 2017 00:00:00", "country": "Canada", } ]; let sectors = json.map((obj) => { return obj.sector }) let countries = json.map((obj) => { return obj.country }) console.log('sectors:', sectors) console.log('countries:', countries)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.