Steven
Steven

Reputation: 1596

Immutable-JS: Merge a List of Maps by key

I have an app-state object, which sort of looks like this:

const initialState = Immutable.fromJS({
    $isLoading: true,
    currentPage: 0,
    totalPages: 1,
    results: []
});

When I fetch from server (using fetch API), I want to set $isLoading to false, and merge currentPage and totalPages in from the response. That's easy enough so far using merge. I'm not quite sure how to elegantly merge in the results node. They're Immutable.Maps, and should merge based on the _id property. Is there an elegant way to work it into this bit of code?

return state.merge({
    $isLoading: false,
    currentPage: res.page,
    totalPages: res.pages,
    results: 'something' // Take old results, merge with res.results
});

Upvotes: 4

Views: 9527

Answers (1)

user5004821
user5004821

Reputation:

You should just be able to change your merge function to mergeDeep:

https://facebook.github.io/immutable-js/docs/#/Map/mergeDeep

Upvotes: 4

Related Questions