用法
- find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
- 如果没有符合条件的元素返回 undefined
- find() 对于空数组,函数是不会执行的。
- find() 并没有改变数组的原始值。
- array.find(function(currentValue, index, arr),thisValue),其中currentValue为当前项,index为当前索引,arr为当前数组
实例
let test = [1, 2, 3, 4, 5];
let a = test.find(item => item > 3);
console.log(a); //4
let b = test.find(item => item == 0);
console.log(b); //undefined
//一般用于数据字典回显和数据查找 vue用法 unitColorData为筛选的数组数据
setUnitColor(name) {
let row = this.unitColorData.find((v) => v.textMap.NAME == name)
if (row) {
return `color: ${row.textMap.FONT_COLOR_HEX};background:${row.textMap.BG_COLOR_HEX}`
} else {
return `color: #138085; background:${this.hexToRgba('#138085', 0.1)}`
}
},