Check out this example that demonstrates the use of basic JavaScript:
let items = [],
products = [{ name: 'A' }, { name: 'B' }, { name: 'C' }];
let item = search(products, function(obj) { return obj.name === 'A'; });
if (item) items.push(item);
function search(array, condition) {
for (let i = 0; i < array.length; i++)
if (condition(array[i])) { return array[i]; break; }
}
https://codepen.io/examples/5421/
You have the freedom to implement any custom logic once you find an object in the array. In this demonstration, I am simply appending it to the 'items' array.
If you prefer to utilize some of the predefined methods available in the Array object, explore this alternative approach:
function search(array, property, value) {
let idx = products.map(function(obj) { return obj[property]; }).indexOf(value);
return array[idx];
}
It's important to consider browser compatibility when implementing such functionality.
Alternatively, you can leverage a utility library like Lodash that provides comprehensive tools for these types of tasks.