0

I am dispatching an action that will update the values in the redux state and I am passing an array. But before that, I want to update the array by changing the object values but I keep getting an error - Cannot assign to read only property 'position' of object

I am not sure why I am getting this error because I am creating a new Array and updating the values in that.

Code

export const GetCustomers= (customers) => (dispatch, getState) => {
  let activeCustomers = Array.from(customers);
  for (let i = 0; i < activeCustomers.length; i += 1) {
     activeCustomers[i].position = i;
  }

  dispatch(actions.updateActiveCustomers(activeCustomers));
  );
};

Getting an error - Cannot assign to read only property 'position' of object

I am not sure why I am getting this error because I am creating a new Array and updating the values in that.

3
  • Array.from only makes a shallow copy, so activeCustomers[i] is the same as customers[i] Commented Aug 18, 2020 at 16:45
  • What happens when you log activeCustomers after Array.from .. ? Commented Aug 18, 2020 at 16:47
  • @RobinZigmond How do I make it a deepCopy? Commented Aug 18, 2020 at 16:53

1 Answer 1

1

To deep copy use:

  let activeCustomers = JSON.parse(JSON.stringify(customers));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.