[英]How to update only one object property or multiple in an array using ngrx state in Angular 5?
I have an array of elements which I am storing in a state, the array would have look something like this: 我有一个要存储在状态中的元素数组,该数组看起来像这样:
filterList: Array<any> = [
{name: "Branch", filters: [{value: "Location 1", status: "unselected"}, {value: "Location 2", status: "selected"}]},
{name: "Segment", filters: [{value: "Segment 1", status: "selected"}, {value: "Segment 2", status: "selected"}]},
...... and so on
]
The array is a huge list, where I would like to add an object to the filters
array or change the status
property from unselected
to selected
or even add more objects to the filterList
. 该数组是一个巨大的列表,我想在其中添加一个对象到filters
数组,或者将status
属性从unselected
更改为selected
,甚至将更多对象添加到filterList
。
How do I set the state, to only change the things I needed and not the entire array? 如何设置状态,仅更改所需的内容,而不更改整个数组? As of now, I have been trying few things, but couldn't work them out. 截至目前,我一直在尝试一些尝试,但无法解决。 Here is the one I tried. 这是我尝试过的。
case FilterActions.FILTER_LIST:
return {
...state,
filterList: [...state, action.payload]
};
And also how do I dispatch the actions to update only a few elements at a time? 另外,我该如何调度动作来一次仅更新几个元素? I have been trying to do it like this. 我一直在尝试这样做。
this.store.dispatch(new MainActions.FilterListActions([{name: "Branch"}, filters: [{value: "Location 1", status: "selected"}]]));
Please help me out this. 请帮我解决这个问题。
Update 更新
Here's the code for store.actions.ts
这是store.actions.ts
的代码
import {Action} from '@ngrx/store';
export const FILTER_LIST = 'FILTER_LIST';
export class FilterListAction implements Action {
readonly type = FILTER_LIST;
constructor (public payload: Array<any>) {}
}
export type FilterActions = FilterListAction;
Here's the code for the store.reducer.ts
file: 这是store.reducer.ts
文件的代码:
import * as FilterActions from './store.actions';
const initialState = {
filterList: []
};
export function filtersReducer(state = initialState, action: FilterActions.FilterActions) {
switch (action.type) {
case FilterActions.FILTER_LIST:
return {
...state,
filterList: [...state, action.payload]
};
default: return state;
}
}
I belive you can use simple js/ts function 'find' to get this done. 我相信您可以使用简单的js / ts函数“查找”来完成此操作。
let filterList: Array<any> = [
{name: "Branch", filters: [{value: "Location 1", status: "unselected"}, {value: "Location 2", status: "selected"}]},
{name: "Segment", filters: [{value: "Segment 1", status: "selected"}, {value: "Segment 2", status: "selected"}]},
...... and so on
]
consider the case where you need to added object 考虑需要添加对象的情况
let toBeAddedFilter = {value: "Location 1", status: "unselected"}
to filters
where name= "Branch"
. filters
name= "Branch"
。
Then you use filter()
like 然后您使用filter()
像
filterList.find(x=>x.name=="Branch").filters.push(toBeAddedFilter);
also to change status of one specific location of a specific branch 还可以更改特定分支的一个特定位置的状态
filterList.find(x=>x.name=="Branch")
.filters.find(y=>y.value="Location 1").status ='selected';
I belive you can make a function for each need out of this example. 我相信您可以在此示例中为每种需要创建一个函数。 This does not change the complete array. 这不会更改整个数组。
To complete Pass the value of the filtercategory name using a function to your custom array update function if the filter category change. 完成如果过滤器类别发生更改,请使用函数将filtercategory名称的值传递给自定义数组更新函数。 if it is something inside filters of one filter category pass that too. 如果它属于某个过滤器类别的过滤器中的内容,则也可以通过。 the functions can be like okThisChanged(filtercategory,value,newStatus)
for edit and okThisFilterAdded(filtercategory,newlyAddedFilter)
for add 函数可以像okThisChanged(filtercategory,value,newStatus)
进行编辑,而okThisFilterAdded(filtercategory,newlyAddedFilter)
进行添加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.