封装数组中遍历 自定义key value

使用方法如下
this.$utils.pub.getResult(this.getResult, 1, 'key', 'value')
封装如下

util.getResult = function (arr = [], id = '', find = 'id', result = 'value') {
  let value = ''
  if (id === undefined || id === null || !find || !result) {
    return ''
  }
  arr.map(function (e) {
    if (e[find] === id) {
      value = e[result]
    }
  })
  return value
}

猜你喜欢

转载自blog.csdn.net/qq_39517820/article/details/80510048