I have a dynamic JSON data which changes the API could return a single object or an array of data, when the API returns a single object, I get an error on the loop and data map. But I tried not applying a forEach to data and get the data individually and store them it the data. I get an error of data.map is not a function. I have a bar graph and I want to display the data below.
"City": {
"Town": "Texas",
"Number": "12"
}
I have the code below which display the data which directs to the forEach and data.map. The code displays correctly if it returns a number of data.
data.forEach((d) => {
d.town = d.Town;
d.number = +d.Number;
});
const xScale = d3.scale.ordinal()
.domain(data.map(d => d.town))
.rangeRoundBands([0, width], 0.1);
const yScale = d3.scale.linear()
.domain([0, d3.max(data, d => d.number)])
.range([height, 0]);
How can I be able to handle dynamic data within my graph whether the API return a single or multiple values or data?
Array.isArray()to test the value before trying to use.map()data.town.