Can't access array of deconstructed variables with higher order function

I am currently having a issue with the following code. When I attempt to deconstruct the world object for some of its properties and then floor the values, I get a initialization error.

(https://replit.com/@Michael_Nicol/Issue#index.js)

const world = {
  mazeIndex: 0,
  startZ: 1.5,
  startY: 2,
  startX: 2.5,
  endZ: 0,
  endY: 1.34543,
  endX: 3.1
};
function generateCoordMaze() {
  let { startZ, startY, startX, endZ, endY, endX } = world
  [startZ, startY, startX, endZ, endY, endX].map(c => Math.floor(c)) 
  console.log([startZ, startY, startX, endZ, endY, endX])
}
generateCoordMaze()

The reason I need to do this is because world is actually the object for a dat.gui controller, and each of its properties will be slider values.

You are missing a semicolon ; after world

ecma262: 12.9.3.1 Interesting Cases of Automatic Semicolon Insertion in Statement Lists

https://www.google.com/search?q=ASI+automatic+semicolon+insertion

1 Like

How come the map or forEach functions do not floor the values?

 [startZ, startY, startX, endZ, endY, endX].forEach((c) => (c = Math.floor(c)));

I am setting each value of c to the floored value.

That is not how forEach works

const numbers = [2, 4, 6];

numbers.forEach(number => number = number * 2);
console.log(numbers); // [ 2, 4, 6 ]

Use a map and capture the new array.

const numbers = [2, 4, 6];

const numbersTimesTwo = numbers.map(number => number * 2)
console.log(numbersTimesTwo); // [ 4, 8, 12 ]
1 Like