Why does this function variable have initial values?

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)
+4
source share
2 answers

This is really amazing ... the problem is that when called, the logbrowser takes note of the objects, but not their contents (during the call to the log).

If you want to see the contents of objects during a call, use instead console.log(JSON.stringify(x)).

+4
source

Array.splice() , . , . , .

+1

Source: https://habr.com/ru/post/1691368/


All Articles