Vue.js 数组包含判断:includes() 方法详解
在 Vue.js 中,可以使用 Array.prototype.includes() 方法来判断数组是否包含某个元素。
假设有一个数组 arr,我们想要判断数组中是否包含元素 item,可以使用以下方式:
if (arr.includes(item)) {
// 数组中包含元素 item
} else {
// 数组中不包含元素 item
}
注意:Array.prototype.includes() 方法是 ES2016 新增的方法,不是所有的浏览器都支持。如果需要兼容旧版本的浏览器,可以使用其他方法来判断数组是否包含某个元素,比如使用 Array.prototype.indexOf() 方法或者使用循环遍历数组来判断。
原文地址: https://www.cveoy.top/t/topic/pbmT 著作权归作者所有。请勿转载和采集!