Sortowanie tablic z zagniezdzonymi obiektami

0

Czesc. Glowie sie nad jednym problemem, niestety co raz to inny error natykam.
Mam array obiektow-pracownikow. Kazdy pracownik ma property handedDocuments - moze tam nie byc nic, a moze byc kilka dokumentow.
Aktualnie wyswietlam sobie te dane w tabelce, pracownikow mam posortowanych po startDate, ale handedDocuments wyswietlane sa w takiej kolejnosci w jakiej byly dodane.
Pomoglby mi ktos posortowac handed documents, aby byly wedlug daty poustawiane?

Probowalem sort() w map() probowalem map() w sort() i juz mi sie kroki myla...

[
    {
            "handedDocuments" : [
              {
                "handedDate": "2021-02-01",
                "document": "Passport"
              },
              {
                "handedDate": "2023-05-01",
                "document": "ID card"
              },
              {
                "handedDate": "2022-02-01",
                "document": "Utility bill"
              }
           ],
            "id": "1",
            "name": "Stefan",
            "startDate": "2021-01-01",
            "endDate": "2024-01-01"
    },
    {
            "handedDocuments" : []
            "id": "2",
            "name": "Michal",
            "startDate": "2022-01-01",
            "endDate": "2025-01-01"
    },
]
1

Hej, sądzę że metoda sort powinna dać radę. Nie wiem dokładnie co chcesz z tym zrobić, czy zmutować istniejący obiekt. Spróbuj coś takiego:

const handedDocuments = [
    {
        "handedDate": "2021-02-01",
        "document": "Passport"
    },
    {
        "handedDate": "2023-05-01",
        "document": "ID card"
    },
    {
        "handedDate": "2022-02-01",
        "document": "Utility bill"
    }
]

const sortedHandedDocuments = handedDocuments.sort((a,b) => new Date(a.handedDate).getTime() - new Date(b.handedDate).getTime())

Pozdro!

2

Okej, jeśli chciałbyś zmutować istniejący obiekt to użyłbym jeszcze metody map w następujący sposób:

const employees = [
    {
        "handedDocuments": [
            {
                "handedDate": "2021-02-01",
                "document": "Passport"
            },
            {
                "handedDate": "2023-05-01",
                "document": "ID card"
            },
            {
                "handedDate": "2022-02-01",
                "document": "Utility bill"
            }
        ],
        "id": "1",
        "name": "Stefan",
        "startDate": "2021-01-01",
        "endDate": "2024-01-01"
    },
    {
        "handedDocuments": [],
        "id": "2",
        "name": "Michal",
        "startDate": "2022-01-01",
        "endDate": "2025-01-01"
    }
].map((employee) => {
    employee.handedDocuments = employee.handedDocuments.sort((a,b) => new Date(a.handedDate).getTime() - new Date(b.handedDate).getTime())
    return employee
})
0

podrzucam fragment kodu

useEffect(() => {
  if (!fetchedData.length) return;
  const filteredEmps = fetchedData.filter(
    (employee) => employee.name == currentSelection.name
  );

  // tutaj filteredEmps to tablica z 7-10 obiektami
  
  const aa = filteredEmps.map((employee) => {
    employee.handedDocuments = employee.handedDocuments.sort((a, b) => { //na te linijke wskazuje mi blad ktory wyzej wkleilem
      return new Date(a.handedDate).getTime() - new Date(b.handedDate).getTime());
    });
    return employee;
  });

  console.log(aa);
  // tu bedzie setState

1 użytkowników online, w tym zalogowanych: 0, gości: 1