Finds an object in an array that contains a specific key-value pair.

Version
  • 2.0.0

Example

// Example usage:
const data = [
  {
    id: 1,
    name: 'John',
    children: [
      {
        id: 2,
        name: 'Jane',
        details: { key: 'info', value: 'targetValue' }
      },
      {
        id: 3,
        name: 'Joe'
      }
    ]
  }
];

const result = findObject(data, 'value', 'targetValue', true);
console.log(result); // { key: 'info', value: 'targetValue' }