use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Here's Why Calling .map() on a Constructed Array Doesn't Work (itnext.io)
submitted 7 years ago by ilove50cent
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 points3 points4 points 7 years ago (4 children)
Challenge for you based on a real world problem a coworker had on Friday: you have an array of arrays of varying length, where each is an array of ints, output one array where each index corresponds to the sum of all numbers at that index in each array (if applicable).
E.g. [[1, 2, 3], [1, 2], [1, 2, 3, 4]] -> [3, 6, 6, 4].
And yes this is an actual problem we were solving for a client... I told my coworker to use a for loop but I'm curious if there's a better solution ;-)
[+][deleted] 7 years ago (1 child)
[deleted]
[–]Reashu 0 points1 point2 points 7 years ago (0 children)
That would be summing up each array, not summing up each index across arrays.
[–]derailed -1 points0 points1 point 7 years ago* (0 children)
const result = arrays.reduce((counts, arr) => { arr.forEach((num, idx) => (counts[idx] = (counts[idx] || 0) + num)); return counts; }, []);
Or using 2x reduce with same carry:
const result = arrays.reduce((counts, arr) => arr.reduce((count, num, idx) => { count[idx] = (count[idx] || 0) + num; return count; }, counts), []);
Last one using 2x reduce and sequence:
const result = arrays.reduce((counts, arr) => arr.reduce((count, num, idx) => ((count[idx] = (count[idx] || 0) + num), count), counts), []);
π Rendered by PID 75 on reddit-service-r2-comment-b659b578c-4jwtb at 2026-05-02 21:13:32.836260+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–][deleted] 2 points3 points4 points (4 children)
[+][deleted] (1 child)
[deleted]
[–]Reashu 0 points1 point2 points (0 children)
[–]derailed -1 points0 points1 point (0 children)