Skip to content

Instantly share code, notes, and snippets.

@p0o
Created September 26, 2016 13:52
Show Gist options
  • Select an option

  • Save p0o/9188fb5c3effb3045125e3092423aa74 to your computer and use it in GitHub Desktop.

Select an option

Save p0o/9188fb5c3effb3045125e3092423aa74 to your computer and use it in GitHub Desktop.
Adding item to a List[] Using Set() data structure and returning a serializable array - To be used in JSON state managers like Redux
/**
* Adding item to a List[] Using Set() data structure and returning a serializable array
* @param {String} newItem to be added
* @param {Array} list of current array
* @returns {Array} New item added without repetition
*/
const addToSet = (newItem, list) => {
const itemsSet = new Set();
const newList = [];
list.forEach(listItem => itemsSet.add(listItem));
itemsSet.add(newItem);
itemsSet.forEach(item => newList.push(item));
return newList;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment