在Redux中的React中更新数组中对象的值

问题描述

我正在调度将更新redux状态下的值的操作,并且正在传递数组。但是在此之前,我想通过更改对象值来更新数组,但是我一直遇到错误- 无法分配为只读对象的属性“位置”

我不确定为什么会收到此错误,因为我正在创建一个新的Array并更新其中的值。

代码

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));
  );
};

遇到错误- 无法分配为只读对象的属性“位置”

我不确定为什么会收到此错误,因为我正在创建一个新的Array并更新其中的值。

解决方法

要进行深度复制,请使用:

  let activeCustomers = JSON.parse(JSON.stringify(customers));