Skip to content

Instantly share code, notes, and snippets.

@webdesignberlin
Created May 6, 2022 15:35
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 webdesignberlin/55c0cb04502fe3fd714e0bf4d5aad2cc to your computer and use it in GitHub Desktop.
Save webdesignberlin/55c0cb04502fe3fd714e0bf4d5aad2cc to your computer and use it in GitHub Desktop.
Add / Remove Item to/from Array via watch
watch(arrayToWatch, (newVal, oldVal = []) => {
const difference = newVal?.filter(x => !oldVal?.includes(x)) || [];
console.log('difference', difference);
const symmetricDifference = newVal?.filter((x) => !oldVal?.includes(x))
.concat(oldVal?.filter((x) => !newVal?.includes(x))) || [];
console.log('symetricDifference', symmetricDifference);
if (difference?.length > 0) {
emit('add-item', difference[0]);
} else {
emit('remove-item', symmetricDifference[0]);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment