prevent mutations in the original data by using the object assign and map problem

The problem is caused by following lines:

x: (e.position.x -= bbX / 2 - parent.x),
y: (e.position.y -= bbY / 2 - parent.y)

When you use subtraction assignment operator -=, you also assign the result to the left variable which is the orignal data, just subtraction operator - would be fine:

const data = [{id:"a", position:{x:10,y:10}, parent:'3a'},{id:"b", position:{x:100,y:100}}];

const newData = data.reduce((arr, cur) => {
      let e = Object.assign({}, cur, {position: {x: (cur.position.x - 5),y: (cur.position.y - 5)}});
      return arr;

console.log("original data:" + JSON.stringify(data));

console.log("new data:" + JSON.stringify(newData));

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top