Compare an Array to an Array of Objects and Return a new Array of Objects with Grouped Dates

Ethan

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

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Compare an array with an array of objects

分類Dev

Mapping array of objects into new array of array by value

分類Dev

'push' new number into an array of objects

分類Dev

Compare 2 array's of objects and find matching color id's then create a new array Javascript

分類Dev

Mapping array to array of objects

分類Dev

Array into an array of objects in Java

分類Dev

Sort array of objects based on new data

分類Dev

filter array of objects with underscore and get new array of values

分類Dev

Sort array of objects based on inner array of objects

分類Dev

Convert array of objects to array of arrays

分類Dev

transform an array of json objects to array

分類Dev

Convert Array of objects to Array in angularjs

分類Dev

For loop sum of numbers between specific dates in array of objects

分類Dev

LoDash - DeepFlatten array of objects

分類Dev

Array of objects intersection

分類Dev

Ruby array of objects

分類Dev

multidimensional array of objects in angular

分類Dev

Mongoose not saving array of objects

分類Dev

Shifting elements in an Array of objects

分類Dev

Mongodb update array of objects

分類Dev

Splice function on an array of objects

分類Dev

Check the duplicate objects in array

分類Dev

Check if an array of objects exists

分類Dev

Search nested array of objects

分類Dev

javascript array of objects - redistribute

分類Dev

Render an array of objects react

分類Dev

Changing the css of objects in a array

分類Dev

passing an parameter to an array of objects

分類Dev

Javascript, Array of Objects?