Skip to content

Instantly share code, notes, and snippets.

@ryankshaw
Created August 15, 2023 00:33
Show Gist options
  • Save ryankshaw/39848fb5d71af2f918d55d441b3a817a to your computer and use it in GitHub Desktop.
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?
// 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