# 根据对象的键值在数组中查找对象

# 语法

import { findObjectInArray } from 'jtoolset';
const result = findObjectInArray(arr, key, value);

# 参数

  • arr (Array) : 待查找的目标数组。
  • key (String) : 需要查找的对象的 key
  • value (String | number) : 需要查找的对象的 value

# 返回值

Obj : 返回查找到的对象。

# 源码

const findObjectInArray = (arr, key, value) => arr.find((obj) => obj[key] === value);

# 例子

import { findObjectInArray } from 'jtoolset';
const fruits = [
  { name: 'Bananas', quantity: 5 },
  { name: 'Apples', quantity: 10 },
  { name: 'Grapes', quantity: 2 },
];
const result = findObjectInArray(fruits, 'name', 'Apples');
console.log(result); //=> {name: 'Apples', quantity: 10}