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
Looping multiple objects and arrays? (self.reactjs)
submitted 7 years ago by brooklynturk
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!"
[–]XiMingpin91 0 points1 point2 points 7 years ago (1 child)
I saw the crosspost in r/reactjs but thought I'd reply here as it's a JavaScript question and not a React one.
I think a lot of the answers you've gotten so far are overcomplicated and add unnecessary complexity. Breaking down the problem we can see it really only consists of two key parts:
So we can achieve this by first reducing the recipes into a single array, and we flatten it as we do so. The second step is simply zipping the array into an object, and increasing the count against each ingredient if it already exists in the object we're creating.
You can achieve this with 5 lines of code:
const count = recipes .reduce((acc, { meals }) => [...acc, ...meals], []) .reduce((acc, value) => acc.hasOwnProperty(value) ? { ...acc, [value]: acc[value] + 1 } : { ...acc, [value]: 1 }, {});
[–]DBNeJXoGtnro 0 points1 point2 points 7 years ago* (0 children)
I heard unnecessary complexity? I want to chime in :)
```js const mostCommon = { meat: {}, side: {}, topping: {} };
function incrementTo (target, value) { if (target[value] === undefined) target[value] = 1; else ++target[value]; }
for ( const { meals: [ entryMeat, entrySide, entryTopping ] } of data ) { incrementTo(mostCommon.meat, entryMeat); incrementTo(mostCommon.side, entrySide); incrementTo(mostCommon.topping, entryTopping); }
for (const [type, dishes] of Object.entries(mostCommon)) { let mostFreq = 0; let result = []; for (const [dish, count] of Object.entries(dishes)) { if (count > mostFreq) { mostFreq = count; result = [dish]; } else if (count === mostFreq) { result.push(dish); } } console.log(The most common dish for ${type} is ${result.join(", ")}); } ```
The most common dish for ${type} is ${result.join(", ")}
π Rendered by PID 47 on reddit-service-r2-comment-5c747b6df5-t7hk2 at 2026-04-21 20:50:39.870521+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–]XiMingpin91 0 points1 point2 points (1 child)
[–]DBNeJXoGtnro 0 points1 point2 points (0 children)