Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Created March 17, 2022 10:54
Show Gist options
  • Save Avi-E-Koenig/283356532666345ac441ed42bfba6136 to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/283356532666345ac441ed42bfba6136 to your computer and use it in GitHub Desktop.
a basic reducer doodle
const baseArr = [[1, 2], [{ foo: 'bar' }]]
const reducer1 = (keepRefs) => (acc, val, i) => {
if (keepRefs) {
acc[i] = val
return acc
}
try {
acc[i] = JSON.parse(JSON.stringify(val))
} catch (error) {
acc[i] = val
}
return acc
}
const resWithRefs = baseArr.reduce(reducer1(true), {})
const resWithoutRefs = baseArr.reduce(reducer1(false), {})
console.log("🚀 ~ file: derp.js ~ line 19 ~ resWithoutRefs", {
resWithRefs, resWithoutRefs,
refTest: resWithRefs[1] === baseArr[1],
noRefTest: resWithoutRefs[1] === baseArr[1]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment