[英]VueJS - How to check an array on matching values?
I'm working on a couponcode
VueJS app, in which I want to check an array with different discountcodes
on matching values.我正在开发一个
couponcode
券couponcode
VueJS 应用程序,我想在其中检查具有不同discountcodes
的数组的匹配值。 Below I have an array with two discountcodes
.下面我有一个带有两个
discountcodes
的数组。 If the button is clicked, I want to check the array for any matches.如果单击按钮,我想检查数组是否有任何匹配项。 I am not sure what would be the best solution for this..
我不确定什么是最好的解决方案。
<template>
<div class="container">
<input placeholder='type discount' v-model="discountInput">
<button @click="checkDiscount">check for discount</button>
<span class="alert" v-if="discountValid">
Code juist
</span>
<span class="alert" v-if="discountInvalid">
Code onjuist
</span>
</div>
</template>
<script>
export default {
props: {
},
data: () => {
return {
discountInput: '',
discountValid: false,
discountInvalid: false,
discountCodes: [
{ code: 'discount-code-1', message: '10% discount' },
{ code: 'discount-code-2', message: '5 dollar discount' }
]
}
},
components: {
},
methods: {
checkDiscount() {
if (this.discountInput === this.discountCode) {
return true;
} else {
return false;
}
}
},
watch: {
}
}
</script>
checkDiscount() {
if (this.discountCodes.find(x => x.code === this.discountInput)) {
return true;
} else {
return false;
}
}
or as comments pointed out could be reduced to:或者正如评论指出的那样可以简化为:
checkDiscount() {
return !!this.discountCodes.find(x => x.code === this.discountInput);
}
Try to use array some
method as follows :尝试使用数组的
some
方法如下:
checkDiscount() {
return this.discountCodes.some(dis => dis.code === this.discountInput)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.