FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Hard

Data Selection

40mins

Implement a function selectData(rows, predicate) that filters rows of data based on a dynamic predicate.

The function should:

  • Accept an array of objects (rows) representing a dataset.
  • Accept a predicate function or an object specifying multiple conditions.
  • Support deep property access (e.g., 'user.address.city').
  • Allow combining conditions using AND / OR logic.
  • Handle arrays and nested objects within the rows gracefully.
  • Return a new array of rows matching all conditions.

Examples

const data = [ { id: 1, user: { name: 'Alice', age: 25 } }, { id: 2, user: { name: 'Bob', age: 30 } }, { id: 3, user: { name: 'Charlie', age: 30 } }, ]; // Using a predicate function selectData(data, row => row.user.age === 30); // returns [{ id: 2, ... }, { id: 3, ... }] // Using a conditions object selectData(data, { 'user.age': 30, 'user.name': 'Bob' }); // returns [{ id: 2, user: { name: 'Bob', age: 30 } }]

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests