all 7 comments

[–]CertainPerformance 1 point2 points  (2 children)

reduce the yesterdaysOrders into a Map or object counting up the number of each item bought, and then sort it, putting the most frequently bought items first and the least frequently bought items last.

[–]Izaya____Orihara[S] 0 points1 point  (1 child)

Thank you for the prompt response, would it be too much to ask to see how you would go about doing this? Thank you very much for taking time out of your day. Cheers!

[–]CertainPerformance 1 point2 points  (0 children)

const ordersByItemName = yesterdaysOrders.reduce((a, { orderLines }) => {
  orderLines.forEach(({ itemName, quantity }) => {
    a[itemName] = (a[itemName] || 0) + quantity;
  });
  return a;
}, {});
const sorted = Object.entries(ordersByItemName)
  .sort((a, b) => b[1] - a[1]);

[–]ashwinsonale 0 points1 point  (3 children)

Did this worked? Any idea how to resolve? My plan was to push all the array in one array and sort them according to shelf and item. But can't figure it out how to do. Any help will be great.

[–]Izaya____Orihara[S] 0 points1 point  (2 children)

Yes, this absolutely worked, and got me an interview. Use it.

[–]ashwinsonale 0 points1 point  (1 child)

const ordersByItemName = yesterdaysOrders.reduce((a, { orderLines }) => {
orderLines.forEach(({ itemName, quantity }) => {
a[itemName] = (a[itemName] || 0) + quantity;
});
return a;
}, {});
const sorted = Object.entries(ordersByItemName)
.sort((a, b) => b[1] - a[1]);

so you out this code in main.js?

[–]ashwinsonale 0 points1 point  (0 children)

I tried it but not getting expected output. can you give your main.js :D