Created
July 9, 2018 04:01
-
-
Save hwindo/e3cf4bb93b2df446ef2309bdfe1c806a to your computer and use it in GitHub Desktop.
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
/** | |
* addItem | |
* adding item into existing array by matching id | |
*/ | |
function addItem(item, currentArrayOfItem) { | |
let exist = currentArrayOfItem.find(_item => _item.id === item.id); | |
if (exist) { | |
let index = currentArrayOfItem.indexOf(exist); | |
currentArrayOfItem.splice(index, 1, item); // reason: reactivity | |
} else { | |
currentArrayOfItem.unshift(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment