从处于Redux状态的对象中添加或删除对象

时间:2019-03-27 10:53:35

标签: javascript reactjs redux react-redux

我是react-redux减速器的新手,这里有一个对象,它是来自API调用的响应。

因此,我已将其保存为jobList状态。现在,我得到的响应对象是这样的:

{
   "jdId":"5c987c171251350001330f72",
   "userName":"abc",
   "location":{
      "weightage":1,
      "quickWeightage":1,
      "criteria":"abcc"
   },
   "mustHaveSkills":{
      "technologies":[
         {
            "weightage":0.5,
            "quickWeightage":1,
            "criteria":"OOPS"
         },
         {
            "weightage":0.5,
            "quickWeightage":1,
            "criteria":"OOPS"
         }
      ],
      "functionalSkills":[
         {
            "weightage":0.5,
            "quickWeightage":1,
            "criteria":"OOPS"
         },
         {
            "weightage":0.5,
            "quickWeightage":1,
            "criteria":"OOPS"
         },
         {
            "weightage":0.5,
            "quickWeightage":1,
            "criteria":"OOPS"
         },

      ]
   },
   "shouldHaveSkills":{
      "technologies":[
         {
            "weightage":0.35,
            "quickWeightage":0.7,
            "criteria":"C"
         },
         {
            "weightage":0.35,
            "quickWeightage":0.7,
            "criteria":"C"
         }
      ],
      "functionalSkills":[

      ]
   },
   "couldHaveSkills":{
      "technologies":[

      ],
      "functionalSkills":[

      ]
   },
   "goodToHaveSkills":{
      "technologies":[
         {
            "weightage":0.25,
            "quickWeightage":0.5,
            "criteria":"XAMARIN"
         }
      ],
      "functionalSkills":[

      ]
   }
}

现在,这里我已经写了一些动作,

export const add(obj, type) {   
    return {
       type: ADD_DATA,
       payload: obj
    }
}

现在,在减速器中:

 case ADD_DATE : {

// Here I want to update this object which is having a an object for the 
 //mustHaveSkills which is an key with array of objects. and I want to add that new object in this array. I also have the type as this key has two arrays in it which to be updated.
}

现在,我正在传递一个要更新为

的对象
{
  "weightage": 0.5,
  "quickWeightage": 1,
  "criteria": "OOPS"
}

type作为Technology,我为需要更新的四个键分别编写了动作。因此,例如,我需要更新mustHaveSkills

因此,现在我如何才能在化简器状态下更新该特定对象? 任何帮助都会有所帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

如果要更新technologies的{​​{1}}部分,则可以按照

mustHaveSkills

考虑到case ADD_DATA: return { ...state, mustHaveSkills: { ...state.mustHaveSkills, technologies: [...mustHaveSkills.technologies, action.payload], } } 包含此数据的事实

action.payload

并且还假设{ "weightage": 0.5, "quickWeightage": 1, "criteria": "OOPS" } 对象结构就像您发布的响应对象一样。