I am trying to access all values in an array of objects. My data structure is like this:
var data = [
obj{
x: 1,
y: 2
},
obj{
x:3,
y:9
}
]
I would like to get values of every x at once.
I tried to use a for loop and then assign values to global variables like this:
var var startValue;
var startYValue;
var startXValue;
var endYValue;
var endXValue;
var endValue;
for (var i = 0; i < data.length; i++) {
endYValue = data[i].endY;
endXValue = data[i].endX;
endValue = { x:endYValue, y:endXValue }
startYValue = data[i].startY;
startXValue = data[i].startX;
startValue = { x:data[i].startY, y:data[i].startX }
console.log(endValue.x)
}
console.log(endValue.x)
And I found out the first console.log(endValue.x) worked but when I console.log(endValue.x) globally, it only showed the last x value instead of all x values.
Any help would be much appreciated!!