Created
May 6, 2022 15:35
-
-
Save webdesignberlin/55c0cb04502fe3fd714e0bf4d5aad2cc to your computer and use it in GitHub Desktop.
Add / Remove Item to/from Array via watch
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
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