I'm trying to fetch data then store them in different arrays. But somehow my result array items are undefined
.
Here is the code:
const xData = [];
const yData = [];
const dataStorage = [];
fetch('https://canvasjs.com/services/data/datapoints.php')
.then(response => response.json()
.then(data => { // data is an array
data.forEach((element) => {
dataStorage.push({x: element[0], y: element[1]});
xData.push(element[0]);
yData.push(element[1]);
})
}));
console.log(xData[0]) // undefined
Could someone please help explain this to me? Much appreciated!