Skip to curriculum
Jump to the editor

23.1. Select and filter fields from records

Easy
Topics
10–20 min

A table of users, an admin report, a CSV export: they all ask the same question of a list of records. Which rows, and which columns? A database answers it with SELECT name, email FROM users WHERE city = 'Pune' LIMIT 10. On the client you often answer it yourself, over data that is already loaded.

Write select(records, options) in select.js. It returns a new array with the records that match where, each reduced to the fields named in fields, and at most limit of them.

Signature

JAVASCRIPT
function select(records, options) // returns a new array of new objects
  • records: an array of plain objects.
  • options.fields: an array of field names to keep. When it is missing, every own field is kept.
  • options.where: either an object of field: value pairs, or a function (record, index) => boolean. When it is missing, every record matches.
  • options.limit: the most rows to return. When it is missing, there is no limit.
  • options itself may be left out.

Requirements

  1. Each row holds only the fields in fields, with its keys in the order of fields.
  2. Without fields, each row is a copy of the whole record, never the record itself.
  3. With a where object, a record matches when it has every listed field as its own field and each one equals its value. Equality is strict (===), with no type conversion, so 0 matches -0; the one exception is that NaN matches NaN. A record without a listed field does not match, even when the value is undefined or an inherited property such as toString would be equal. An empty where object matches every record.
  4. With a where function, a record matches when the function returns a true value. It is called with the record and its index.
  5. A field the record does not have is left out of the row (no key set to undefined). A field the record has with the value undefined is kept. Inherited properties, such as toString, never count as fields. Any own field name is kept as a key of the row, __proto__ included.
  6. limit counts matching rows. Once it is reached, select stops reading records (and stops calling where).
  7. select never changes records, the records in it, or options.

Examples

JAVASCRIPT
const people = [
  { id: 1, name: "Asha", city: "Pune", age: 31 },
  { id: 2, name: "Ben", city: "Leeds", age: 24 },
  { id: 3, name: "Chen", city: "Pune", age: 45 },
];

select(people, { fields: ["name", "age"], where: { city: "Pune" } });
// [{ name: "Asha", age: 31 }, { name: "Chen", age: 45 }]
JAVASCRIPT
select(people, { fields: ["id"], where: (person) => person.age > 30, limit: 1 });
// [{ id: 1 }]
JAVASCRIPT
select([{ id: 1, email: "a@x.test" }, { id: 2 }], { fields: ["id", "email"] });
// [{ id: 1, email: "a@x.test" }, { id: 2 }]: the second row has no email key

Environment

  • Plain JavaScript in a browser page; the tests call the global select from select.js.
  • Field names are plain keys ("city"), not paths ("address.city"). Sorting and grouping are not part of this problem.
  • Any built-in is allowed.

Hints

  1. How can one function handle both kinds of where, so the loop does not care which one it got?
  2. "toString" in {} is true. Which check is true only for the object's own fields?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…