vue判断值是否存在数组
时间: 2023-07-31 14:08:55 浏览: 77
input输入值正则校验及使用方式举例.txt
可以使用Array的`includes()`方法来判断一个值是否存在于数组中。例如:
```javascript
const array = ['apple', 'banana', 'orange'];
const value = 'banana';
if (array.includes(value)) {
console.log(`${value} exists in the array.`);
} else {
console.log(`${value} does not exist in the array.`);
}
```
输出结果为:
```
banana exists in the array.
```
如果要判断多个值是否存在于数组中,可以使用`every()`方法结合`includes()`方法,例如:
```javascript
const array = ['apple', 'banana', 'orange'];
const values = ['banana', 'pear'];
if (values.every(value => array.includes(value))) {
console.log(`All values exist in the array.`);
} else {
console.log(`Not all values exist in the array.`);
}
```
输出结果为:
```
Not all values exist in the array.
```
阅读全文