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"
}
}
],
"",
"",
""
],
]
You are on the right track, except for few minor tweaks
forEach is better suited
. Pick the values and push into new array.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.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加