0

What I want to have is nested data grouped by the day number.

This is an example of a array that I want to group. I am using the lodash plugin.

[{
  "Pnl": 29.0035635,
  "date": "11/14/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
},
{
  "Pnl": 50.8878545,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 73.1014552,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 32.477,
  "date": "11/08/2022",
  "dayNumber": 6,
  "translationDayOfWeek": "Saturday"
},
{
  "Pnl": 25.43999561,
  "date": "09/30/2022",
  "dayNumber": 5,
  "translationDayOfWeek": "Friday"
},
{
  "Pnl": 17.6294068,
  "date": "09/30/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
}]

This is want I want for a output:

[
  {
    "dayNumber": 1,
    "orders": [
      {
        "Pnl": 29.0035635,
        "date": "11/14/2022",
        "dayNumber": 1,
        "translationDayOfWeek": "Monday"
      },
      {
        "Pnl": 17.6294068,
        "date": "09/30/2022",
        "dayNumber": 1,
        "translationDayOfWeek": "Monday"
      }
    ]
  },
  {
    "dayNumber": 2,
    "orders": [
      {
        "Pnl": 50.8878545,
        "date": "11/08/2022",
        "dayNumber": 2,
        "translationDayOfWeek": "Tuesday"
      },
      {
        "Pnl": 73.1014552,
        "date": "11/08/2022",
        "dayNumber": 2,
        "translationDayOfWeek": "Tuesday"
      }
    ]
  }
]

I tried the solutions on stackoverflow post but it's not the result I need.

jarmod
  • 71,565
  • 16
  • 115
  • 122
ChahinDev
  • 36
  • 5
  • 1
    weeknumber isn't in the data, and if the result ends up with a different structure, like with orders, then it isn't just a sort. – danh Nov 24 '22 at 01:25
  • please clarify more about sort means how's it working... – Jerry Nov 24 '22 at 01:30
  • You are looking for group, not sort. See https://stackoverflow.com/questions/70844485/how-to-group-array-with-multiple-properties, that is not the same as you need, but some concept is the same – Manuel Romeiro Nov 24 '22 at 01:34
  • Please edit your post because the output you're showing is not a differently sorted version of the input. It's a complete rewrite, so [in more detail](/help/how-to-ask) explain what part of the rewriting process is a problem. Also, don't just link to another post and go "this didn't work", explain what from that post you did, what you expected it to do, and how what it actually did differed _and what debugging you already did_ because all those things change what an answer should look like. – Mike 'Pomax' Kamermans Nov 24 '22 at 01:35
  • @Mike'Pomax'Kamermans Yes, I should put a better description on my post. I am new here on StackOverflow but thanks for the recommendation. – ChahinDev Nov 24 '22 at 01:40

3 Answers3

2

If you are using loadsh,

const arr = []; //your array
_.map(
    _.groupBy(arr, function (obj) {return obj.dayNumber}),
    (order,index) => ({dayNumber: index, orders: order})
)
subodhkalika
  • 1,964
  • 1
  • 9
  • 15
0

The snippet below does grouping (which is what it looks like the OP wants) using reduce().

let data = [{
  "Pnl": 29.0035635,
  "date": "11/14/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
},
{
  "Pnl": 50.8878545,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 73.1014552,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 32.477,
  "date": "11/08/2022",
  "dayNumber": 6,
  "translationDayOfWeek": "Saturday"
},
{
  "Pnl": 25.43999561,
  "date": "09/30/2022",
  "dayNumber": 5,
  "translationDayOfWeek": "Friday"
},
{
  "Pnl": 17.6294068,
  "date": "09/30/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
}];

let grouped = data.reduce((acc, el) => {
  let dayNumber = el.dayNumber;
  acc[dayNumber] ??= { dayNumber, orders: [] };
  acc[dayNumber].orders.push(el);
  return acc;
}, {});
grouped = Object.values(grouped).sort((a,b) => a.dayNumber-b.dayNumber);

console.log(grouped)

It's possible that the OP also wants the orders array sorted somehow. But I can't discern how. To do that, iterate the result and do the sort, something like the following:

grouped.forEach(group => group.orders.sort((a,b) => {
  /* sort based on some prop the OP hasn't defined */
});
danh
  • 62,181
  • 10
  • 95
  • 136
  • This code worked out for me. Thanks. I have a question about row 358. What is the '??=' doing in this code? Is this a typescript operator? – ChahinDev Nov 24 '22 at 01:47
  • did you google it? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment – MauricioRobayo Nov 24 '22 at 01:49
  • It's a thing called a "nullish coalescing assignment. The gist is it will only assign if the left side is null or undefined. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment – danh Nov 24 '22 at 01:49
0

Try this one

const arr = [{
  "Pnl": 29.0035635,
  "date": "11/14/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
},
{
  "Pnl": 50.8878545,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 73.1014552,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 32.477,
  "date": "11/08/2022",
  "dayNumber": 6,
  "translationDayOfWeek": "Saturday"
},
{
  "Pnl": 25.43999561,
  "date": "09/30/2022",
  "dayNumber": 5,
  "translationDayOfWeek": "Friday"
},
{
  "Pnl": 17.6294068,
  "date": "09/30/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
}];
const newArr = [];

arr.forEach((i) => {
    if (newArr.find(j => j.dayNumber === i.dayNumber)){
        const index = newArr.findIndex(j => j.dayNumber === i.dayNumber)
        newArr[index].orders.push(i);
    }
    else {
        newArr.push({
            dayNumber: i.dayNumber,
            orders: [i]
        })
    }
})

newArr.sort((a, b) => parseInt(a.dayNumber) - parseInt(b.dayNumber))

console.log(newArr)
Shakya Peiris
  • 504
  • 5
  • 11