Skip to content

Instantly share code, notes, and snippets.

@hwindo
Created July 9, 2018 04:01
Show Gist options
  • Save hwindo/e3cf4bb93b2df446ef2309bdfe1c806a to your computer and use it in GitHub Desktop.
Save hwindo/e3cf4bb93b2df446ef2309bdfe1c806a to your computer and use it in GitHub Desktop.
/**
* 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