简体   繁体   English

当相应项目中没有值时,从 Redux 初始 state 中删除完整的 object

[英]remove full object from Redux initial state when there is no values in the corresponding items

In my reducer, I have the initial state which looks like this:在我的减速器中,我有初始的 state,如下所示:

    const initialState = {
        isLoading: false,
        events: [
        {
        year: 2021,
        place: [
        {
          id: 1,
          name: "BD"
        },
        {
          id: 2,
          name: "BD Test"
        }
      ]
    },
    { year: 2020, place: [{ id: 3, name: "AMS" }, { id: 4, name: "AMS TEST" }] }
  ]
};

I have been trying to implement the functionality of deletion operation.我一直在尝试实现删除操作的功能。 So, when the button will be clicked the "deleteItems" action will be dispatched that will remove the corresponding items from the place .因此,当单击按钮时,将调度“deleteItems”操作,该操作将从place中删除相应的项目。 This functionality works fine.此功能工作正常。 But,I am trying to remove the whole items from the events array if there is no values in place .但是,如果没有place的值,我会尝试从events数组中删除整个项目。

This is what I have tried already but it just removes the individual place .这是我已经尝试过的,但它只是删除了个别place But, I need to write the logic here of removing the whole items when place becomes empty.但是,我需要在这里编写当place变空时删除整个项目的逻辑。

case "deleteItems":
  return {
    ...state,
    events: state.events.map(event => {
      const place = event.place.find(x => x.id === action.id);
      if (place) {
        return {
          ...event,
          place: event.place.filter(x => x.id !== action.id)
        };
      }
      return event;
    })
  };

So, after modifications, the state would look like this:(when there is no values in place for year 2021)因此,在修改后,state 将如下所示:(当 2021 年没有值时)

const initialState = {
  isLoading: false,
  events: [
    { year: 2020, place: [{ id: 3, name: "AMS" }, { id: 4, name: "AMS TEST" }] }
  ]
};

Does anybody know how to accomplish this.有谁知道如何做到这一点。 Any helps would be highly appreciated.Thanks in Advance.任何帮助将不胜感激。在此先感谢。 Demo can be seen from here Demo可以从这里看到

I removed the places first.我先删除了这些地方。 Then I filtered events based on whether the place array is empty or not.然后我根据位置数组是否为空来过滤事件。 After that, I returned the state.之后,我退回了 state。

case "deleteItems":
      const eventsPostDeletingPlaces = state.events.map(event => {
        const place = event.place.find(x => x.id === action.id);
        if (place) {
          return {
            ...event,
            place: event.place.filter(x => x.id !== action.id)
          };
        }
        return event;
      });
      const eventsWithPlaces = eventsPostDeletingPlaces.filter((each) => each.place.length);
      return {
        ...state,
        events: eventsWithPlaces
      }

Check the edited sandbox here此处检查已编辑的沙箱

Basically the same logic as in the first answer, but with reduce instead of a map and an extra filter .与第一个答案中的逻辑基本相同,但使用reduce而不是map和额外的filter Just an option.只是一种选择。

case "deleteItems":
  return {
    ...state,
    events: state.events.reduce((events, event) => {
      const place = event.place.find(x => x.id === action.id);

      if (place) {
        event.place = event.place.filter(x => x.id !== action.id);
      }

      if (event.place.length > 0) {
        events.push(event);
      }

      return events;
    }, [])
  };

codesandbox 密码箱

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM