【问题标题】:Deleting Object in Redux State with Id in Lodash/Javascript在 Lodash/Javascript 中使用 Id 删除 Redux 状态下的对象
【发布时间】:2016-12-25 00:06:24
【问题描述】:

我指的是 solution 从 redux 状态中删除不需要的 ID。我的状态设置略有不同,我不确定如何使用 lodash 过滤它。

我的数组:

[
  {id:1, animal:fish, name:bob}, 
  {id:2, animal:dig,name:jim}, 
  {id:3,animal:cat, name:Tk} 
]

这是我的违规代码(我将 ID 作为 action.payload 传递):

const INITIAL_STATE = { animals:[]};

export default function(state = INITIAL_STATE, action) {
  switch (action.type) {
    case DELETE_ANIMAL:
        let animalId = action.data;
        return {...state, animals: [...state.filter(c => c.id !== animalId)]; 
  }
  return state;
}

【问题讨论】:

    标签: object redux state store lodash


    【解决方案1】:

    我认为您不需要为此使用 lodash。只需过滤动物数组:

    case DELETE_ANIMAL:
            let animalId = action.data;
            return {...state, animals: state.animals.filter(c => c.id !== animalId)}
    

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多