1

let points = [{x:1,y:2},{x:2,y:2}];
let centroidsPoints = [1,2].map(() => []);
console.log(centroidsPoints); // => [[],[]]
let centroidsPointsCopy = [...centroidsPoints]; //copy array
points.forEach(point => {
  centroidsPointsCopy[0].push(point);
});
console.log(centroidsPointsCopy); // => [[{x:1,y:2},{x:2,y:2}],[]]
console.log(centroidsPoints); // => [[{x:1,y:2},{x:2,y:2}],[]] but expected [[],[]]

Why don't i get expected behaviour since i copied default array with

[...centroidPoints]

Sorry if duplicate, i only found answer for "array1=array2" copy;

Saffer
  • 152
  • 8

0 Answers0