Skip to content

Instantly share code, notes, and snippets.

@iddan
Created January 8, 2017 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iddan/d2760715473eb42880d300fba4832f30 to your computer and use it in GitHub Desktop.
Save iddan/d2760715473eb42880d300fba4832f30 to your computer and use it in GitHub Desktop.
insert to document collection with unique ids
/**
* 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