When I register arr before this function, it has values (shown below). I expected him to print [[null, null], [null, null]]; instead, it prints the arr value that occurs after this function completes.
updateBounds() {
let arr = [Array(2), Array(2)];
console.log('initial value')
console.log(arr);
arr[0][0] = this.state.homePoint.geometry.coordinates[0];
arr[1][0] = this.state.homePoint.geometry.coordinates[0];
arr[0][1] = this.state.homePoint.geometry.coordinates[1];
arr[1][1] = this.state.homePoint.geometry.coordinates[1];
this.state.points.forEach(p => {
if (p.geometry) {
if (p.geometry.coordinates) {
arr[0][0] = Math.min(p.geometry.coordinates[0], arr[0][0]);
arr[1][0] = Math.max(p.geometry.coordinates[0], arr[1][0]);
arr[0][1] = Math.min(p.geometry.coordinates[1], arr[0][1]);
arr[1][1] = Math.max(p.geometry.coordinates[1], arr[1][1]);
}
}
});
this.setState({bounds: arr});
}
Console Result:
initial value
App.js:444 (2) [Array(2), Array(2)]
0: (2) [-122.438227, 37.742017]
1: (2) [-122.40766, 37.784995]
length: 2
__proto__: Array(0)
source
share