1
2
3
4
5
6
Use the "Verify Solution" button to get AI feedback on your JavaScript code.
Your task is to implement the filterByProperty function. This function should take three arguments:
items: An array of objects.propName: A string representing the name of the property to filter by.targetValue: The value to match against the specified property.The function should return a new array containing only the objects from the input items array where the value of propName matches targetValue. If no objects match, an empty array should be returned.
Example:
const products = [
{ id: 1, name: 'Laptop', category: 'Electronics' },
{ id: 2, name: 'Keyboard', category: 'Electronics' },
{ id: 3, name: 'Mouse', category: 'Peripherals' },
{ id: 4, name: 'Monitor', category: 'Electronics' }
];
filterByProperty(products, 'category', 'Electronics');
// Expected: [{ id: 1, name: 'Laptop', category: 'Electronics' }, { id: 2, name: 'Keyboard', category: 'Electronics' }, { id: 4, name: 'Monitor', category: 'Electronics' }]