findTargetVal

2020/12/28

从数组 arr 中查找字段 id 的值等于 val 的条目。

findTargetVal(val, arr, 'id')

# 参数

  1. val (any) :匹配的值
  2. arr (Array) :数据列表
  3. key (String) :匹配的字段名,默认为 value
  4. multiple (Boolean) :是否查找所有符合的条目,默认是 false

# 返回值

(Object|undefined|Array) :数组中的条目或者数组

# 例子

import { findTargetVal } from '@liz-q/utils'
const arr = [
    { id: '1', type: '1', name: 'foo' },
    { id: '2', type: '1', name: 'bar' },
    { id: '3', type: '2', name: 'tar' }
]

const target = findTargetVal('2', arr, 'id') // { id: '2', name: 'bar' }

const targets = findTargetVal('1', arr, 'type', true)
// [{ id: '1', type: '1', name: 'foo' }, { id: '2', type: '1', name: 'bar' }]
Last Updated: 2023/11/6 上午9:06:55