javascript - 在reactjs中返回带有更新数组的整个对象

标签 javascript reactjs ecmascript-6 redux immutable.js

我在显示其中包含更新的要素数组的要素对象时遇到问题。对于未触及/未删除的部分,它工作正常但我删除的功能仅返回功能 数组而不是显示其中包含特征数组的整个对象。我的意思是,如果我有这样一个对象

0: Object
feature_description: "Room Amenities"
feature_type: "roomamenities"
features:Array(6)
_id:"59395255455a5b043c764a29"

1:Object
feature_name: "Room Amenities"
feature_type: "bathroom"
features:Array(3)
_id:"59395255455a5b043c764a29"

现在当我删除 roomamenities 特征类型的特征时,我得到以下结果

0:Object
features: Array(5) # 5 not 6 because one item is deleted 

1:Object
feature_name: "Room Amenities"
feature_type: "bathroom"
features:Array(3)
_id:"59395255455a5b043c764a29"

相反,它应该返回以下对象

0: Object
feature_description: "Room Amenities"
feature_type: "roomamenities"
features:Array(5)
_id:"59395255455a5b043c764a29"

1:Object
feature_name: "Room Amenities"
feature_type: "bathroom"
features:Array(3)
_id:"59395255455a5b043c764a29"

我该如何解决我的问题?

这是我的reducer代码

case UPDATE_REQUESTED_FEATURES_SUCCESS:
  return state
    .update("features", features =>
      state.get('features').map(feature => {
        console.log("feature", feature);
        if (feature.get("feature_type") === "roomamenities") {
          return {
            ...feature,
            features: feature.get("features").map(subFeature => {
              return subFeature.get("_id") !== action.response.data._id;
            })
          };
        } else {
          return feature;
        }
      })
    );

这是通过 consoling state.get('features') 从 reducer 产生的特征对象的结果

enter image description here

最佳答案

您的 feature 对象是 ImmutableJS 映射。所以替换这个:

  return {
    ...feature,
    features: feature.get("features").map(subFeature => {
      return subFeature.get("_id") !== action.response.data._id;
    })
  };

与:

  return feature.set("features", 
      feature.get("features").filter(subFeature => {
          return subFeature.get("_id") !== action.response.data._id;
      })
  );

关于javascript - 在reactjs中返回带有更新数组的整个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45914977/

相关文章:

javascript - React - 状态属性未定义 - 为什么?

javascript - 获取警告 : React component classes must extend React. 组件。关于 React

reactjs - 如何在 typescript 中正确导入自定义类型

reactjs - TypeScript: 'string | undefined' 不可分配给类型 'string'

javascript - 在调用 setState 之前 react : why state change when try to change it in immutable way ,

javascript - 使用react js、babel和es6导入随机文件

javascript - 将数字值与网页上的单词相关联,然后将其乘以网页上该单词后面的数字

javascript - 如何使用 jquery 更改我的输入类型 ="image"宽度

javascript - 我想知道 Array.prototype.sort 基本上是如何工作的 排序函数的polyfill

javascript - 比较 "this"对象与 DOM 数组中的对象