数组中 includes()方法 : 包含
include()方法:
arr.includes(searchElement)方法:
用来判断一个数组是否包含一个指定的值,
如果是返回 true,否则false。
searchElement:必须。需要查找的元素值。
let site = ['runoob', 'google', 'taobao'];
site.includes('runoob'); // true
site.includes('baidu'); // false
arr.includes(searchElement, fromIndex)。
fromIndex:可选。从该索引处开始查找searchElement。
如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。
例子 结果
[1, 2, 3].includes(2); //true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); //true
[1, 2, NaN].includes(NaN); //true
注意:如果fromIndex 大于等于数组长度 ,则返回 false 。该数组不会被搜索
var arr = ['a', 'b', 'c'];
arr.includes('c', 3); //false
arr.includes('c', 100); // false
注意:
如果 fromIndex 为负值,
计算出的索引将作为开始搜索searchElement的位置。
如果计算出的索引小于 0,则整个数组都会被搜索。
// 数组长度是3
// fromIndex 是 -100
// computed index 是 3 + (-100) = -97
arr.includes(‘a’, -100); // true
arr.includes(‘b’, -100); // true
arr.includes(‘c’, -100); // true
项目实例
需求是这样的,只允许页面中有一个默认值,在点击默认的时候前端也可以做判断,这样就不用去请求接口数据了。当然后端做最好。
代码:
handleDefault = async (keys, rows, type) => {
const defaultDate = this.tableRef.getRows() // 获取查询到的数据结果[]
console.log(defaultDate)
let moren = defaultDate.map((item) => { // 循环每一项
return item.defaultFlag // 返回指定的 默认的值
})
console.log(moren)
if(moren.includes(true)) { // 如果这个默认中中有了true
ElNotification({
type: 'error',
message: '项目中包含已默认状态,请重新配置!'
});
} else {
const res = await service.getProcurementTypeId(rows[0]);
this.handleRequestResult(res);
}
};