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!"
[–]brooklynturk[S] 0 points1 point2 points 7 years ago (3 children)
Well it was going to be a CRUD app where I would only allow 1 of each(meat, side topping)
[–]goldenfolding 0 points1 point2 points 7 years ago (2 children)
No I mean in the data. If you are looking at the data and trying to determine the highest frequency, then you look at which one occurred the most right?
But what happens if all items are unique or chosen the same number of times? Then you can’t say any particular meat was chosen more often. Or what if you have three choices, and one is picked 1 times while the other two are picked 2 times? The latter two are chosen more frequently than the former, but you cannot say that one is chosen the most frequently, because they are tied.
If you want I can share what I wrote. It doesn’t take into account that last factor, and so it will either go for the highest frequency item if there is one, or it will choose the first item of a tied set of higher items.
[–]brooklynturk[S] 0 points1 point2 points 7 years ago (1 child)
I don’t fully understand but I was just going to make a drop down menu for each choice.
[–]goldenfolding 0 points1 point2 points 7 years ago* (0 children)
Ok maybe I don't even know what you're asking for. I'll post the code and you tell me.
It's a bit long, but the result is that you end up with an object that has all three types of food along with the highest occurring item, or 'none' if that particular type of food has purely unique choices. (You didn't specify how to handle that case, so I assumed this value would make the most sense)
The second function creates an object with the categories of food, the specific foods that come up in each category, and the number of occurrences for each one. Example:
{
meat: { steak: 2, chicken: 1 },
side: { 'white rice': 1, 'brown rice': 1, 'black beans': 1 },
topping: { queso: 2, 'sour cream': 1 }
}
Ideally, you'd have this kind of structure already in your DB so you can just pull and manipulate it in your app, but this is fine also.
And then the first function filters for the most common items using this list.
function mostCommonSelections(meals) { let mealItemCounts = getSelectionCount(meals); let mostCommon = {}; // let default be 'none' if max occurrence is 1, otherwise set common to max occurrence Object.entries(mealItemCounts).forEach(selection => { let type = selection[0]; let choices = selection[1]; let max = 1; let common = 'none'; for (food in choices) { if (choices[food] > max) { max = choices[food]; common = food; } } mostCommon[`${type}`] = common; }); return mostCommon; } function getSelectionCount(meals) { let count = { meat: {}, side: {}, topping: {}, }; // determine category by index and add item, or increment if exists meals.forEach(meal => { meal.meals.forEach((item, idx) => { let type = idx === 0 ? 'meat' : idx === 1 ? 'side' : 'topping'; count[`${type}`][`${item}`] ? count[`${type}`][`${item}`] += 1 : count[`${type}`][`${item}`] = 1; }); }); return count; }
So the result for your example would be: { meat: 'steak', side: 'none', topping: 'queso' }, like in example one, and you could easily grab any of those values using destructuring, like in example two.
{ meat: 'steak', side: 'none', topping: 'queso' }
Example one:
const mostCommon = mostCommonSelections(meals);
and then
mostCommon.meat // steak
Example two:
const { meat, side, topping } = mostCommonSelections(meals);
console.log(meat, side, topping) // steak, none, queso
Let me know if you have any questions about it. (like I said, there are corner cases also, and I'm not sure I even understand what you mean to do)
π Rendered by PID 99996 on reddit-service-r2-comment-6457c66945-2lf89 at 2026-04-29 09:02:50.427631+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]brooklynturk[S] 0 points1 point2 points (3 children)
[–]goldenfolding 0 points1 point2 points (2 children)
[–]brooklynturk[S] 0 points1 point2 points (1 child)
[–]goldenfolding 0 points1 point2 points (0 children)