Created
March 17, 2022 10:54
-
-
Save Avi-E-Koenig/283356532666345ac441ed42bfba6136 to your computer and use it in GitHub Desktop.
a basic reducer doodle
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
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