我在显示其中包含更新的要素数组的要素对象时遇到问题。对于未触及/未删除的部分,它工作正常但我删除的功能仅返回功能 数组而不是显示其中包含特征数组的整个对象。我的意思是,如果我有这样一个对象
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 产生的特征对象的结果
最佳答案
您的 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/