JavaScript reduce

up: JavaScript

Minimal conditional reduce

const reduced = arr.reduce((acc, el) => {
  const isSmall = el < 2
  if (isSmall) {
    return acc
  }
  acc.push({ number: el })
  return acc
}, [])
// Expected output: [ { number: 2 }, { number: 3 }, { number: 4 } ]

Conditional reduce into map

  const map = allProjects.value?.reduce((acc, el) => {
    const [category] = el?.category ?? [""]
    if (!category) {
      return acc
    }
    const { codename, name: label } = category
    acc.set(codename, label)
    return acc
  }, new Map())