gpt4 book ai didi

javascript - 更新特征数组的特定对象

转载 作者:行者123 更新时间:2023-11-29 21:03:43 24 4
gpt4 key购买 nike

我正在努力更新特征数组中的特定对象,并用更新后的对象返回整个对象。我可以更新特定的对象,但不能用更新后的对象返回相同的整个对象。我正在使用 immutablejs 执行此操作。这是我的代码

    case EDIT_FEATURE_SUCCESS: {
return state
.set('requesting', false)
.set('response', action.response.data.message)
.update('features', features =>
state.get('features').map(feat => {
return {
...feat,
features: features.map(feature => {
if (feature._id === action.response.data.featureObj._id) {
return action.response.data.featureObj;
} else return feature;
})
};
})
);
}

我需要以下对象

enter image description here

有了更新的,但我得到了这样的东西

enter image description here

最佳答案

immutablejs 在更新嵌套对象时看起来很复杂。你可以试试这个。让我知道它是否有效。在我看来,它应该有效

case EDIT_FEATURE_SUCCESS: {
return state
.set('requesting', false)
.set('response', action.response.data.message)
.update('features', features =>
features.map(feat => {
return {
...feat,
features: feat.features.map(feature => {
if (feature._id === action.response.data.featureObj._id) {
return action.response.data.featureObj;
} else return feature;
})
};
})
);
}

在代码中,我将state.get('features') 更改为features,这只是缩短了代码行。问题是您正在执行 features.map 而不是它应该是 feat.features 因为 feat 将具有您正在更新的功能数组。

关于javascript - 更新特征数组的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com