简体   繁体   English

如何过滤反应 redux 减速器 state 的阵列?

[英]How can I filter an array which is in react redux reducer state?

This is my reducer file in react这是我在 react 中的 reducer 文件

const initialState = {
    products: [
        {
            name: 'Icecream',
            inCart: false,
            num: 2
        },
        {
            name: 'Cake',
            inCart: true,
            num: 5
        }
    ]
};

export const reducer = (state = initialState, action) => {
    switch (action.type) {
        case REVERSE:
            let newProd = [ ...state.products ];

            newProd.filter((a) => {
                if (a.inCart === true) {
                    return a;
                }
            });

            console.log(newProd);

            return {
                ...state,
                products: newProd
            };
        default:
            return state;
    }
};

and this is the console log of newProd which shows the filter function doesn't work这是 newProd 的控制台日志,显示过滤器 function 不起作用

0: {name: "Icecream", inCart: false, num: 2}
1: {name: "Cake", inCart: true, num: 5}
length: 2
__proto__: Array(0)

How can I filter the products array so I can only get the item that has inCart = true and replace it with the old products array?如何过滤 products 数组,以便我只能获取具有 inCart = true 的项目并将其替换为旧 products 数组?

Filter returns a new array & won't mutate the original one.过滤器返回一个新数组并且不会改变原始数组。 You are filtering but not assigning the output of filter to a variable.您正在过滤但未将过滤器的 output 分配给变量。

Do this做这个

export const reducer = (state = initialState, action) => {
    switch (action.type) {
        case REVERSE:
            let newProd = state.products.filter((a) => { //<---- like this
                if (a.inCart === true) {
                    return a;
                }
            });

            console.log(newProd);

            return {
                ...state,
                products: newProd
            };
        default:
            return state;
    }
};

You can also do it in a single liner - like this:您也可以在单个衬垫中执行此操作 - 如下所示:

...
case REVERSE:
    return {
        ...state,
        products: state.products.filter(a => a.inCart)
    };
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应 redux 减速器 state 中更改数组中的 object 值? - How can I change an object value in an array in react redux reducer state? 我不能在Redux reducer中使用state.filter - I can't use state.filter in Redux reducer 我如何从 React-Redux 中的 reducer 中的状态更新对象数组中单个元素的名称字段? - How can i update a name field from single element in array of object from a state in reducer in React-Redux? 如何在 ReactJS 的 Reducer 中更新 redux 状态? - How can I update redux state in Reducer in ReactJS? 如何在 redux 减速器文件中为 state 设置输入值? - How can I set input value to the state in redux reducer file? 如何在 redux reducer 中改变数组的状态? - How to mutate a array on state in a redux reducer? 在React / Redux reducer中,如何以不可变的方式更新嵌套数组中的字符串? - In React/Redux reducer, how can I update a string in an nested array in an immutable way? 我如何从 Redux/react 中的数组 state 中删除 object - How can i remove an object from an array state in Redux/react React-Redux:从减速器 function 中的 state 复制一个数组,以便您可以修改它 - React-Redux: Copying an array from the state in Reducer function so that you can modify it 我如何在 react redux 中访问另一个 reducer 的状态。 reducer 和 store 之间的状态流 - How do i access state of one reducer in other in react redux. State flow between reducers and store
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM