NGRX - 删除密钥的 updateMany
NGRX - updateMany with key deletion
我想使用实体适配器中的 updateMany 从对象中删除特定键。
假设我有一个对象数组:
[{id:1,key1:"test",key2:"test2"},{id:2,key1:"xxx",key2:"yyy"}]
我想从实体中完全删除 key2。
如何实现?
我尝试使用更新对象数组调度操作:
[{id: 1, changes: { [key2]: null } },{id: 2, changes: { [key2]: null } }]
然后在减速器中:
on(MyActions.updateCollection, (state, action) => {
adapter.updateMany(action.updates, {
...state})})
但是上面的方法我将 key2 设置为空,而不是删除它。知道如何实现吗?
您需要通过向那里传递一个没有密钥的更新实体来使用 upsertOne
/ upsertMany
。要删除密钥,您可以使用这样的 reduce 函数:
const objWithoutTheKey = Object.keys(objWithKey).reduce((result, key) => {
if (key !== 'keyToDelete') {
result[key] = objWithKey[key];
}
return result;
}, {});
我想使用实体适配器中的 updateMany 从对象中删除特定键。
假设我有一个对象数组:
[{id:1,key1:"test",key2:"test2"},{id:2,key1:"xxx",key2:"yyy"}]
我想从实体中完全删除 key2。
如何实现?
我尝试使用更新对象数组调度操作:
[{id: 1, changes: { [key2]: null } },{id: 2, changes: { [key2]: null } }]
然后在减速器中:
on(MyActions.updateCollection, (state, action) => {
adapter.updateMany(action.updates, {
...state})})
但是上面的方法我将 key2 设置为空,而不是删除它。知道如何实现吗?
您需要通过向那里传递一个没有密钥的更新实体来使用 upsertOne
/ upsertMany
。要删除密钥,您可以使用这样的 reduce 函数:
const objWithoutTheKey = Object.keys(objWithKey).reduce((result, key) => {
if (key !== 'keyToDelete') {
result[key] = objWithKey[key];
}
return result;
}, {});