0

I messed with it for hours

What is the best way to manipulate/convert this array of objects:

const arr = [
{
    _id: "5fb70c9244fa59c7dc1d9151",
    date: "2020-11-22T09:00:00.000Z",
    determinedBy: {
        _id: "man1",
        name: "manA",
    },
    hour: "10:00",
    massageType: "mass",
},
{
    _id: "2",
    date: "2020-11-22T09:00:00.000Z",
    determinedBy: {
        _id: "man2",
        name: "manB",
    },
    hour: "11:00",
    massageType: "thay",
},
{
    _id: "3",
    date: "2020-12-05T09:00:00.000Z",
    determinedBy: {
        _id: "man3",
        name: "manC",
    },
    hour: "20:00",
    massageType: "chi",
},

to this structure:

  const objManipulate={
  2020-11-22T09:00:00.000Z:[
   {
    _id: "5fb70c9244fa59c7dc1d9151",
    determinedBy: {
        _id: "5fac1f65a9e40443ccdf274b",
        name: "a",
    },
    hour: "10:00",
    massageType: "mass",
  },
  {
    _id: "2",
    date: "2020-11-22T09:00:00.000Z",
    determinedBy: {
        _id: "2222222",
        name: "sssssa",
    },
    hour: "11:00",
    massageType: "thay",
  }
]
"2020-12-05T09:00:00.000Z:[
     {
        _id: "3",
        determinedBy: {
            _id: "3333333",
            name: "threee",
        },
        hour: "20:00",
       massageType: "chi",
    },
  ]

as u see:

  • key - > its by date day (the hour doesn't matter, I will convert the date after to showing MM/DD/YYYY, no problem with this)
  • value -> array of objects of all meetings in this day.

-I need this to show the data in the react-native-calendar in right way, note that I tried with a for loop, map, filter, but that didn't work.

react_or_angluar
  • 1,568
  • 2
  • 13
  • 20
Roei Grinshpan
  • 395
  • 1
  • 5
  • 19
  • 2
    This question has both a lodash answer *and* a plain JS answer for grouping by a key value: https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key – Jayce444 Nov 20 '20 at 01:35
  • I found there a lot of answers that response exactlly of what i needed. Thanks man. How i know which function is the best way between reduce\groupby\for each\map? Or it doesnt matter – Roei Grinshpan Nov 20 '20 at 01:46

0 Answers0