Created
September 26, 2016 13:52
-
-
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
This file contains hidden or 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
| /** | |
| * 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