Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

81%
+7 −0
Challenges Truthify an array

JavaScript (Node.js), 74 70 bytes Works for any number of dimensions. f=(a,z=[])=>a.map?.((b,c)=>z.push(...f(b).map(a=>[c,...a])))?z:a?[z]:z Try it online! (TIO uses an old version ...

posted 4y ago by Hakerh400‭  ·  edited 4y ago by Hakerh400‭

Answer
#4: Post edited by user avatar Hakerh400‭ · 2021-01-09T15:30:07Z (almost 4 years ago)
  • # [JavaScript (Node.js)], <del>74</del> 70 bytes
  • <!-- language-all: lang-javascript -->
  • f=(a,z=[])=>a.map?.((b,c)=>z.push(...f(b).map(a=>[c,...a])))?z:a?[z]:z
  • [Try it online!](https://tio.run/##VVCxbsMgEN39FYyHRBGsjoj3DunQETEQ16SuEmMZEin8vAOYxraQ7h7v3b2D@9MP7dqpH/3HYH@6eTYCNAlCKiyOmt702FCAM2njNdDx7n6BUmrgjJMGWhxlSyKjFca4CbVuZFB1mFs7OI9857xDAskKIUZi4ClIlSNbEi@JMMLzKfyO2VKKvDPbiv8NWecFMcI2PQlFuPSlQUVnxYuvrtlXyveTcuWCVm7vuZ20Vu7d02PVoaqMnWDdEbJm2RWOP0m0vXb0ai/w@f11os5P/XDpzRMMpKq46cP8Ag) (TIO uses an old version of Node.js that does not support the `?.` operator, but you can try the [74 bytes](https://tio.run/##VVCxjsMgDN3zFYxYogjWVmn3DnfDjYjBJVClakMVuEr9@hwQrkmEZD89Pz8b3/CFwYz9M@4G39lpci22R@QPfJ6Qj7b7NZbSCzOsg/Z44cYPBiN11EDW0CRWHeOcowZgSsMeT0ppvVd6StoQSbQhBtIS1RAiWAoyB6VLFHOSNTHBZHmV3zBrSrNPFuvif0Opy4oEE6uejBKc@/KgWhfVSy6uxVepz0pFOaOF23quJy3KrXteVh@axvmRLjci3s23gvSTTPu75Xd/peef7y8e4tgP19690@mzCgAO0x8) solution)
  • [JavaScript (Node.js)]: https://nodejs.org
  • # [JavaScript (Node.js)], <del>74</del> 70 bytes
  • Works for any number of dimensions.
  • <!-- language-all: lang-javascript -->
  • f=(a,z=[])=>a.map?.((b,c)=>z.push(...f(b).map(a=>[c,...a])))?z:a?[z]:z
  • [Try it online!](https://tio.run/##VVCxbsMgEN39FYyHRBGsjoj3DunQETEQ16SuEmMZEin8vAOYxraQ7h7v3b2D@9MP7dqpH/3HYH@6eTYCNAlCKiyOmt702FCAM2njNdDx7n6BUmrgjJMGWhxlSyKjFca4CbVuZFB1mFs7OI9857xDAskKIUZi4ClIlSNbEi@JMMLzKfyO2VKKvDPbiv8NWecFMcI2PQlFuPSlQUVnxYuvrtlXyveTcuWCVm7vuZ20Vu7d02PVoaqMnWDdEbJm2RWOP0m0vXb0ai/w@f11os5P/XDpzRMMpKq46cP8Ag) (TIO uses an old version of Node.js that does not support the `?.` operator, but you can try the [74 bytes](https://tio.run/##VVCxjsMgDN3zFYxYogjWVmn3DnfDjYjBJVClakMVuEr9@hwQrkmEZD89Pz8b3/CFwYz9M@4G39lpci22R@QPfJ6Qj7b7NZbSCzOsg/Z44cYPBiN11EDW0CRWHeOcowZgSsMeT0ppvVd6StoQSbQhBtIS1RAiWAoyB6VLFHOSNTHBZHmV3zBrSrNPFuvif0Opy4oEE6uejBKc@/KgWhfVSy6uxVepz0pFOaOF23quJy3KrXteVh@axvmRLjci3s23gvSTTPu75Xd/peef7y8e4tgP19690@mzCgAO0x8) solution)
  • [JavaScript (Node.js)]: https://nodejs.org
#3: Post edited by user avatar Hakerh400‭ · 2021-01-09T15:27:40Z (almost 4 years ago)
  • # [JavaScript (Node.js)], 74 bytes
  • Works for any number of dimensions.
  • <!-- language-all: lang-javascript -->
  • f=a=>a.map?a.reduce((b,c,d)=>b.concat(f(c).map(a=>[d,...a])),[]):a?[[]]:[]
  • [Try it online!][TIO-kjpjo3sd]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kjpjo3sd]: https://tio.run/##VVCxjsMgDN3zFYxYogjWVmn3DnfDjYjBJVClakMVuEr9@hwQrkmEZD89Pz8b3/CFwYz9M@4G39lpci22R@QPfJ6Qj7b7NZbSCzOsg/Z44cYPBiN11EDW0CRWHeOcowZgSsMeT0ppvVd6StoQSbQhBtIS1RAiWAoyB6VLFHOSNTHBZHmV3zBrSrNPFuvif0Opy4oEE6uejBKc@/KgWhfVSy6uxVepz0pFOaOF23quJy3KrXteVh@axvmRLjci3s23gvSTTPu75Xd/peef7y8e4tgP19690@mzCgAO0x8 "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], <del>74</del> 70 bytes
  • <!-- language-all: lang-javascript -->
  • f=(a,z=[])=>a.map?.((b,c)=>z.push(...f(b).map(a=>[c,...a])))?z:a?[z]:z
  • [Try it online!](https://tio.run/##VVCxbsMgEN39FYyHRBGsjoj3DunQETEQ16SuEmMZEin8vAOYxraQ7h7v3b2D@9MP7dqpH/3HYH@6eTYCNAlCKiyOmt702FCAM2njNdDx7n6BUmrgjJMGWhxlSyKjFca4CbVuZFB1mFs7OI9857xDAskKIUZi4ClIlSNbEi@JMMLzKfyO2VKKvDPbiv8NWecFMcI2PQlFuPSlQUVnxYuvrtlXyveTcuWCVm7vuZ20Vu7d02PVoaqMnWDdEbJm2RWOP0m0vXb0ai/w@f11os5P/XDpzRMMpKq46cP8Ag) (TIO uses an old version of Node.js that does not support the `?.` operator, but you can try the [74 bytes](https://tio.run/##VVCxjsMgDN3zFYxYogjWVmn3DnfDjYjBJVClakMVuEr9@hwQrkmEZD89Pz8b3/CFwYz9M@4G39lpci22R@QPfJ6Qj7b7NZbSCzOsg/Z44cYPBiN11EDW0CRWHeOcowZgSsMeT0ppvVd6StoQSbQhBtIS1RAiWAoyB6VLFHOSNTHBZHmV3zBrSrNPFuvif0Opy4oEE6uejBKc@/KgWhfVSy6uxVepz0pFOaOF23quJy3KrXteVh@axvmRLjci3s23gvSTTPu75Xd/peef7y8e4tgP19690@mzCgAO0x8) solution)
  • [JavaScript (Node.js)]: https://nodejs.org
#2: Post edited by user avatar Hakerh400‭ · 2021-01-09T10:12:08Z (almost 4 years ago)
  • # [JavaScript (Node.js)], 74 bytes
  • Works for any number of dimensions.
  • <!-- language-all: lang-javascript -->
  • f=a=>a.map?a.reduce((b,c,d)=>b.concat(f(c).map(a=>[d,...a])),m=[]):a?[m]:m
  • [Try it online!][TIO-kjpjcqsm]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kjpjcqsm]: https://tio.run/##jY8xD4MgEIX3/hJIrgRXG3Xv0A4dCcOJaGxEjNom/fUWRasujWG4F@5778ET39iptmz6c20zPQx5hFGMzGCTIGt19lKakBQUZDSKU6ZsrbAnOVF0ZIiDRQaMMZSUgomEpCEmwsjQDI7tbKVZZQtyfdxvrOvbsi7K/OP8IgAOwXS4s9LL6T@@448bJPwmP25dyiZ3MCsOfJM4Kid9qstd9nxuCtbOsRWEWB7vSa/Wu33mtmkl9@n@K8MX "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], 74 bytes
  • Works for any number of dimensions.
  • <!-- language-all: lang-javascript -->
  • f=a=>a.map?a.reduce((b,c,d)=>b.concat(f(c).map(a=>[d,...a])),[]):a?[[]]:[]
  • [Try it online!][TIO-kjpjo3sd]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kjpjo3sd]: https://tio.run/##VVCxjsMgDN3zFYxYogjWVmn3DnfDjYjBJVClakMVuEr9@hwQrkmEZD89Pz8b3/CFwYz9M@4G39lpci22R@QPfJ6Qj7b7NZbSCzOsg/Z44cYPBiN11EDW0CRWHeOcowZgSsMeT0ppvVd6StoQSbQhBtIS1RAiWAoyB6VLFHOSNTHBZHmV3zBrSrNPFuvif0Opy4oEE6uejBKc@/KgWhfVSy6uxVepz0pFOaOF23quJy3KrXteVh@axvmRLjci3s23gvSTTPu75Xd/peef7y8e4tgP19690@mzCgAO0x8 "JavaScript (Node.js) – Try It Online"
#1: Initial revision by user avatar Hakerh400‭ · 2021-01-09T10:03:49Z (almost 4 years ago)
# [JavaScript (Node.js)], 74 bytes

Works for any number of dimensions.

<!-- language-all: lang-javascript -->

    f=a=>a.map?a.reduce((b,c,d)=>b.concat(f(c).map(a=>[d,...a])),m=[]):a?[m]:m

[Try it online!][TIO-kjpjcqsm]

[JavaScript (Node.js)]: https://nodejs.org
[TIO-kjpjcqsm]: https://tio.run/##jY8xD4MgEIX3/hJIrgRXG3Xv0A4dCcOJaGxEjNom/fUWRasujWG4F@5778ET39iptmz6c20zPQx5hFGMzGCTIGt19lKakBQUZDSKU6ZsrbAnOVF0ZIiDRQaMMZSUgomEpCEmwsjQDI7tbKVZZQtyfdxvrOvbsi7K/OP8IgAOwXS4s9LL6T@@448bJPwmP25dyiZ3MCsOfJM4Kid9qstd9nxuCtbOsRWEWB7vSa/Wu33mtmkl9@n@K8MX "JavaScript (Node.js) – Try It Online"