I am trying to iterate over a JSON object and return values to another function.
I would like to pass each value of quadrant1_x, quadrant1_y...quadrant4_y of its respective object to the calculatePlot() function. But I get an error Uncaught TypeError: user is undefined
Javascript looks like this:
function generateRandomUser() {
const user = consumeAPI()
return calculatePlot(user)
}
function consumeAPI() {
const graphs = @json($graphs); //received from the backend (Laravel)
for (let i = 0; i < graphs.length; i++) {
graphs.forEach((value, key) => {
console.log([key, value]);
return {
x1: (value.quadrant1_x * range),
y1: (value.quadrant1_y * range),
x2: (value.quadrant2_x * range),
y2: (value.quadrant2_y * range),
x3: (value.quadrant3_x * range),
y3: (value.quadrant3_y * range),
x4: (value.quadrant4_x * range),
y4: (value.quadrant4_y * range),
}
})
}
function calculatePlot(user) {
return [{
x: center + user.x1,
y: center - user.y1
}, {
x: center + user.x2,
y: center - user.y2
}, {
x: center + user.x3,
y: center - user.y3
}, {
x: center + user.x4,
y: center - user.y4
}]
}
for (let i = 0; i < maxUsers; i++) {
let plot = generateRandomUser();
let label = `Mid-point`;
addUser(plot, label);
calculateMidPoint(plot);
}
This code is generating a cartesian plot as an SVG. So, each quadrant value of x & y is required in order to render a graph.
Sample : JSFiddle

generateRandomUserfunction expects theconsumeAPIfunction to return a user, but it doesn't.