首页 > 解决方案 > NGRX - 如何通过reducer更新数组中对象中的字段?

问题描述

如何通过reducer更新数组中的字段?我试过这个:

const customers = state.filteredCustomers;
for (const customer of customers) {
    for (const addr of customer.addresses) {
         addr.selected = false;
    }
}

return {
    ...state,
    filteredCustomers: customers,
};

但它会抛出一个错误:

 TypeError: Cannot assign to read only property 'selected' of object '[object Object]'

做这个的最好方式是什么?

标签: angularstatengrxreducers

解决方案


import _ from 'lodash';

并在您的代码中使用

const tags: IAreaTag[] = _.cloneDeep(department.tags);

然后返回常量

所以在你的代码中

const customers = _.cloneDeep(state.filteredCustomers);

推荐阅读