2.0.0
Finds an object in an array that contains a specific key-value pair.
The array of objects to search through
The key to match in the objects
The value associated with the key to match
If true, the function will search recursively through nested objects and arrays
The object that matches the key-value pair, or undefined if no match is found
// Example usage:const data = [ { id: 1, name: 'John', children: [ { id: 2, name: 'Jane', details: { key: 'info', value: 'targetValue' } }, { id: 3, name: 'Joe' } ] }];const result = find_object(data, 'value', 'targetValue', true);console.log(result); // { key: 'info', value: 'targetValue' } Copy
// Example usage:const data = [ { id: 1, name: 'John', children: [ { id: 2, name: 'Jane', details: { key: 'info', value: 'targetValue' } }, { id: 3, name: 'Joe' } ] }];const result = find_object(data, 'value', 'targetValue', true);console.log(result); // { key: 'info', value: 'targetValue' }
Version
2.0.0
Remarks
Finds an object in an array that contains a specific key-value pair.
Param: arr
The array of objects to search through
Param: key
The key to match in the objects
Param: value
The value associated with the key to match
Param: recursive
If true, the function will search recursively through nested objects and arrays
Returns
The object that matches the key-value pair, or undefined if no match is found
Example