1

I am trying to format data so that it looks like desiredResult right now its coming out as result. I do realize that I am kinda brute-forcing my way through the data but my algorithm skills are not great. I would appreciate any help trying to fix my code or pointing me in the direction to better understand how I could do this in a more efficient way.

var endResult = dates.map(function (item) {

  var arrayOfEvents = [];     

  var events = arrayOfObjects.map(function (value) {

    if (item === value.info.startDate) {
      arrayOfEvents.push(value)
      return arrayOfEvents
    } else{
      return ""
    }

  });

  return events

});

{JSON.stringify(endResult, null, 4)}

Array

var dates = 
[
"01-06-2020",
"01-07-2020",
"01-08-2020",
"01-10-2020",
"02-04-2020"
]

Array of Objects

var arrayOfObjects =
[
{
    "title": "Group President",
    "id": "TpNY1SU_",
    "info": {
        "description": "hi",
        "eventLocation": "change",
        "dailyActivity": "false",
        "startDate": "01-06-2020"
    }
},
{
    "title": "TEST",
    "id": "cEpPxopz",
    "info": {
        "description": "TEST",
        "eventLocation": "TEST",
        "dailyActivity": "true",
        "startDate": "01-07-2020"
    }
},
{
    "title": "Example",
    "id": "jnTMr_r7",
    "info": {
        "description": "example",
        "eventLocation": "Exmaple",
        "dailyActivity": "true",
        "startDate": "01-07-2020"
    }
},
]

Desired Result

var desiredResult = [
    [
        {
            "title": "Group President",
            "id": "TpNY1SU_",
            "info": {
                "description": "hi",
                "eventLocation": "change",
                "dailyActivity": "false",
                "startDate": "01-06-2020"
            }
        }
    ],
    [
        {
            "title": "TEST",
            "id": "cEpPxopz",
            "info": {
                "description": "TEST",
                "eventLocation": "TEST",
                "dailyActivity": "true",
                "startDate": "01-07-2020"
            }
        },
        {
            "title": "Example",
            "id": "jnTMr_r7",
            "info": {
                "description": "example",
                "eventLocation": "Exmaple",
                "dailyActivity": "true",
                "startDate": "01-07-2020"
            }
        }
    ],
]

Actual Result

var actualResult = [
[
    [
        {
            "title": "Group President",
            "id": "TpNY1SU_",
            "info": {
                "description": "hi",
                "eventLocation": "change",
                "dailyActivity": "false",
                "startDate": "01-06-2020"
            }
        }
    ],
    "",
    "",
    "",
    "",
    ""
],
[
    "",
    [
        {
            "title": "TEST",
            "id": "cEpPxopz",
            "info": {
                "description": "TEST",
                "eventLocation": "TEST",
                "dailyActivity": "true",
                "startDate": "01-07-2020"
            }
        },
        {
            "title": "Example",
            "id": "jnTMr_r7",
            "info": {
                "description": "example",
                "eventLocation": "Exmaple",
                "dailyActivity": "true",
                "startDate": "01-07-2020"
            }
        }
    ],
    [
        {
            "title": "TEST",
            "id": "cEpPxopz",
            "info": {
                "description": "TEST",
                "eventLocation": "TEST",
                "dailyActivity": "true",
                "startDate": "01-07-2020"
            }
        },
        {
            "title": "Example",
            "id": "jnTMr_r7",
            "info": {
                "description": "example",
                "eventLocation": "Exmaple",
                "dailyActivity": "true",
                "startDate": "01-07-2020"
            }
        }
    ],
    "",
    "",
    ""
],
]
Ethan
  • 19
  • 4
  • How does this data structure look to you for your use case? `var desiredResult = { "01-05-2020" : [ ObjectForThisDate1 ], "01-06-2020" : [ ObjectForThisDate1, ObjectForThisDate2 ]......}` – abhinav Jan 08 '20 at 18:39

1 Answers1

0

You are on the right track, except for few minor tweaks

  • You should only use map function when you want to return the array of the same size as original. Here is not the case. The length for data is 5 but your final answer will have 3 items in the array. So, forEach is better suited. Pick the values and push into new array.
    For loop can also be used.
  • The outer and inner map were returning "" which was getting included in the final result (due to map function). Use forEach and push the results into array.
    Read more here: JavaScript: Difference between .forEach() and .map()

  • While using forEach, only the values containing the data need to be pushed in the final result. So, we need to put length check for > 0.

Have modified the code in the snippet below:

var dates = [
  "01-06-2020",
  "01-07-2020",
  "01-08-2020",
  "01-10-2020",
  "02-04-2020"
]

var arrayOfObjects = [{"title":"Group President","id":"TpNY1SU_","info":{"description":"hi","eventLocation":"change","dailyActivity":"false","startDate":"01-06-2020"}},{"title":"TEST","id":"cEpPxopz","info":{"description":"TEST","eventLocation":"TEST","dailyActivity":"true","startDate":"01-07-2020"}},{"title":"Example","id":"jnTMr_r7","info":{"description":"example","eventLocation":"Exmaple","dailyActivity":"true","startDate":"01-07-2020"}}]

var endResult = []
dates.forEach(function(item) {
  var arrayOfEvents = [];
  var events = arrayOfObjects.forEach(function(value) {
    if (item === value.info.startDate) {
      arrayOfEvents.push(value)
    }
  });
  
  if(arrayOfEvents.length > 0) endResult.push(arrayOfEvents)

});

console.log(JSON.stringify(endResult, null, 4))

Hope it helps. Revert for any doubts.

Sunil Chaudhary
  • 4,481
  • 3
  • 22
  • 41
  • Thank you Sunil! This is exactly what I was going for. I have checked your reponse for the correct answer. – Ethan Jan 08 '20 at 19:41