Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Easy testing with JavaScript
(()=>{
const test = (result, expect) =>
`${result === expect && '' || '🔴'} ${expect}:${result}`
function dynamicNestedObject (total, value) {
const result = {}
const keys = 'abcdefghijklmnopqrstuvwxyz'.substring(0, total).split('')
const lastIndex = keys.length - 1
keys.reduce((o, k, i) => {
const isLastIndex = i === lastIndex
const val = isLastIndex && value || o[k] || {}
return o[k] = val
}, result)
return result
}
console.log(dynamicNestedObject(5, 'cool'))
console.log(test(dynamicNestedObject(3, 'hello').a.b.c, 'hello'))
console.log(test(dynamicNestedObject(4, 'world').a.b.c.d, 'world'))
})()
@brianswisher

This comment has been minimized.

Copy link
Owner Author

commented Jul 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.