合并两个不可变映射
Merge two immutable maps
我正在使用 Redux 和 Immutable.js,我正在尝试过滤抽认卡序列并将抽认卡设置为隐藏。然后 return 该状态使用 redux。我的数据模型如下所示:
"flashcardSequence": ["id1", "id2", "id3"],
"flashcards": {
"id1": {
"cgi": "id1",
"creatorGuid": "student1",
"authoringType": "STUDENT",
"sequence": 0,
"hidden": false,
"suppressed": false,
"aspects": {
"123": {
"id": "123",
"role": "Date",
"value": [
{
"type": "text",
"value": "1776"
}
],
"label": [
{
"type": "text",
"value": "Date"
}
]
},
在我的flashcard reducer中,我的代码是这样的:
case types.HIDE_FLASHCARD: {
const index = state
.get('decks')
.findIndex(i => i.get('deckCgi') === action.deckCgi);
const card = state
.getIn(['decks', index, 'flashcards'])
.find(i => i.get('cgi') === action.cardId);
const filteredSequence = state
.getIn(['decks', index, 'flashcardSequence'])
.filter(flashcardId => flashcardId !== action.cardId);
// return state.setIn(
// ['decks', index, 'flashcardSequence'],
// filteredSequence
// );
let filteredSequenceState = state.setIn(
['decks', index, 'flashcardSequence'],
filteredSequence
);
let hiddenCardState = state.setIn(
['decks', index, 'flashcards', action.cardId],
card.set('hidden', true)
);
//I would like to combine both states into one single object without writing over any data.
return filteredSequence + filteredSequenceState;
}
我遇到的问题是如何 return 我的应用程序的两个状态?彼此独立地返回对象效果很好,但我无法合并它们。我试图将它们与提供给我的方法合并,但无济于事。我想 return filteredSequence 和 hiddenCardState。
任何指导将不胜感激。
谢谢。
也许
const filteredSequenceState = state.setIn(
['decks', index, 'flashcardSequence'],
filteredSequence
);
const hiddenCardState = filteredSequenceState.setIn(
['decks', index, 'flashcards', action.cardId],
card.set('hidden', true)
);
return filteredSequenceState;
会如你所愿吗?
旁注:如果您不更改变量,最好使用 const
而不是 let
。
我正在使用 Redux 和 Immutable.js,我正在尝试过滤抽认卡序列并将抽认卡设置为隐藏。然后 return 该状态使用 redux。我的数据模型如下所示:
"flashcardSequence": ["id1", "id2", "id3"],
"flashcards": {
"id1": {
"cgi": "id1",
"creatorGuid": "student1",
"authoringType": "STUDENT",
"sequence": 0,
"hidden": false,
"suppressed": false,
"aspects": {
"123": {
"id": "123",
"role": "Date",
"value": [
{
"type": "text",
"value": "1776"
}
],
"label": [
{
"type": "text",
"value": "Date"
}
]
},
在我的flashcard reducer中,我的代码是这样的:
case types.HIDE_FLASHCARD: {
const index = state
.get('decks')
.findIndex(i => i.get('deckCgi') === action.deckCgi);
const card = state
.getIn(['decks', index, 'flashcards'])
.find(i => i.get('cgi') === action.cardId);
const filteredSequence = state
.getIn(['decks', index, 'flashcardSequence'])
.filter(flashcardId => flashcardId !== action.cardId);
// return state.setIn(
// ['decks', index, 'flashcardSequence'],
// filteredSequence
// );
let filteredSequenceState = state.setIn(
['decks', index, 'flashcardSequence'],
filteredSequence
);
let hiddenCardState = state.setIn(
['decks', index, 'flashcards', action.cardId],
card.set('hidden', true)
);
//I would like to combine both states into one single object without writing over any data.
return filteredSequence + filteredSequenceState;
}
我遇到的问题是如何 return 我的应用程序的两个状态?彼此独立地返回对象效果很好,但我无法合并它们。我试图将它们与提供给我的方法合并,但无济于事。我想 return filteredSequence 和 hiddenCardState。
任何指导将不胜感激。
谢谢。
也许
const filteredSequenceState = state.setIn(
['decks', index, 'flashcardSequence'],
filteredSequence
);
const hiddenCardState = filteredSequenceState.setIn(
['decks', index, 'flashcards', action.cardId],
card.set('hidden', true)
);
return filteredSequenceState;
会如你所愿吗?
旁注:如果您不更改变量,最好使用 const
而不是 let
。