Skip to content

Instantly share code, notes, and snippets.

@AhmedElwerdany
Created October 25, 2022 14:49
Show Gist options
  • Save AhmedElwerdany/ee84922a496b4052d6b6d109b38de27c to your computer and use it in GitHub Desktop.
Save AhmedElwerdany/ee84922a496b4052d6b6d109b38de27c to your computer and use it in GitHub Desktop.
watch array in vue.js v2
// https://ahmedelwerdany.github.io/posts/watch-many-in-vue-js/
function watchMany (arrayOfKeys, value) {
const object = {}
for (let i = 0; i < arrayOfKeys.length; i++) {
object[arrayOfKeys[i]] = value
}
return object
}
export default {
name: 'Form',
data: () => ({
shose_price: 0,
hat_price: 0,
short_price: 0,
}),
watch: {
...watchMany(['shose_price', 'hat_price','short_price'], function (value) {
if (typeof value !== 'number') {
return
}
this.total = this.shoes_price + this.short_price + this.hat_price
}),
total : function () {
this.total_with_disscount = this.total - (this.total * this.discount)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment