The includes() method in the array: contains

include() method:

arr.includes(searchElement) method:
used to determine whether an array contains a specified value,
if so, return true, otherwise false.
searchElement: Required. The element value to look for.

let site = ['runoob', 'google', 'taobao'];
site.includes('runoob');  // true 
site.includes('baidu'); 	// false

arr.includes(searchElement, fromIndex).
fromIndex: optional. Start looking for searchElement at this index.
If negative, search starts from the index of array.length + fromIndex in ascending order. Default is 0.

Example result

[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

Note: Returns false if fromIndex is greater than or equal to the length of the array. The array will not be searched

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

Project instance
The requirement is like this, only one default value is allowed on the page, and the front end can also make judgments when clicking the default, so that there is no need to request interface data. Of course the back end does the best.

insert image description here
Code:

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);
    }
};	

insert image description hereinsert image description here

Guess you like

Origin blog.csdn.net/lzfengquan/article/details/123419778