I am facing a challenge with an array that contains a list of JavaScript objects. My goal is to utilize the reduce function to transform it into a single object with nested key groupings.
This is how the initial array appears:
[ { "product": 1, "date": "2020-01-01", "price": 100 }, { "product": 2, "date": "2020-01-01", "price": 102 }, // Additional objects in the array... ]
My desired end result looks like this:
{ 1:{ "2020-01-01":{ // Object details for product 1 on January 1st } }, 2:{ "2020-01-01":{ // Object details for product 2 on January 1st } // More date-specific details for product 2... } }
Even though I attempted the following approach, I'm only managing to capture a single date per product:
pricelist.reduce((obj, item) => { obj[item['product']] = {}; obj[item['date']][item['product']] = item; return obj; }, {});
If anyone can offer guidance on what I might be missing or doing incorrectly here, I would greatly appreciate it.