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"
}
}
],
"",
"",
""
],
]