Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
23.1. Select and filter fields from records
Easy
Topics
This question is in
23. Text processing and interpretersA 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
function select(records, options) // returns a new array of new objectsrecords: 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 offield: valuepairs, 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.optionsitself may be left out.
Requirements
- Each row holds only the fields in
fields, with its keys in the order offields. - Without
fields, each row is a copy of the whole record, never the record itself. - With a
whereobject, 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, so0matches-0; the one exception is thatNaNmatchesNaN. A record without a listed field does not match, even when the value isundefinedor an inherited property such astoStringwould be equal. An emptywhereobject matches every record. - With a
wherefunction, a record matches when the function returns a true value. It is called with the record and its index. - 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 valueundefinedis kept. Inherited properties, such astoString, never count as fields. Any own field name is kept as a key of the row,__proto__included. limitcounts matching rows. Once it is reached,selectstops reading records (and stops callingwhere).selectnever changesrecords, the records in it, oroptions.
Examples
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 }]select(people, { fields: ["id"], where: (person) => person.age > 30, limit: 1 });
// [{ id: 1 }]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 keyEnvironment
- Plain JavaScript in a browser page; the tests call the global
selectfromselect.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
- How can one function handle both kinds of
where, so the loop does not care which one it got? "toString" in {}istrue. 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…