用法

  1. find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
  2. 如果没有符合条件的元素返回 undefined
  3. find() 对于空数组,函数是不会执行的。
  4. find() 并没有改变数组的原始值。
  5. 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)}`
    }
},

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐