Created
January 8, 2017 15:50
-
-
Save iddan/d2760715473eb42880d300fba4832f30 to your computer and use it in GitHub Desktop.
insert to document collection with unique ids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Insert documents to a document collection with unique ids. Override if exists. | |
* @param {array} state | |
* @param {array} newState | |
* @param {string} id | |
*/ | |
function insert(state, newState, id) { | |
let ids = newState.reduce((ids, item) => ({ ...ids, [item[id]]: true }), {}); | |
return [...state.filter(item => !ids[item[id]]), ...newState]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment