Created
August 15, 2023 00:33
-
-
Save ryankshaw/39848fb5d71af2f918d55d441b3a817a to your computer and use it in GitHub Desktop.
which of the three of these is more readable / has less mental burden to grok?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Which of the 3 of these options is more readable / is easier to understand? | |
// OPTION 1: .forEach with .push | |
const subqueries = [] | |
Object.entries(SPECIAL_SUBQUERY_FIELDS).forEach(([field, subquery]) => { | |
if (fieldRequested(field, info)) | |
subqueries.push(`(${subquery} LIMIT 1) AS ${field}`) | |
}) | |
// OPTION 2: .map with .filter | |
const subqueries = Object.entries(SPECIAL_SUBQUERY_FIELDS) | |
.map( | |
([field, subquery]) => | |
fieldRequested(field, info) && `(${subquery} LIMIT 1) AS ${field}` | |
) | |
.filter(Boolean) | |
// OPTION 3: .flatMap | |
const subqueries = Object.entries(SPECIAL_SUBQUERY_FIELDS).flatMap( | |
([field, subquery]) => | |
fieldRequested(field, info) ? [`(${subquery} LIMIT 1) AS ${field}`] : [] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment