简体   繁体   English

将数组与对象数组进行比较并返回具有分组日期的新对象数组

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

I am trying to format data so that it looks like desiredResult right now its coming out as result .我正在尝试格式化数据,以便它现在看起来像desiredResult其作为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你走在正确的轨道上,除了一些小的调整

  • You should only use map function when you want to return the array of the same size as original.当您想返回与原始大小相同的数组时,您应该只使用 map 函数。 Here is not the case.情况并非如此。 The length for data is 5 but your final answer will have 3 items in the array.数据的长度为 5,但您的最终答案将在数组中包含 3 个项目。 So, forEach is better suited .所以, forEach is better suited Pick the values and push into new array.选择值并推入新数组。
    For loop can also be used .也可以使用 for 循环
  • 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.使用 forEach 并将结果推送到数组中。
    Read more here: JavaScript: Difference between .forEach() and .map()在此处阅读更多信息: JavaScript:.forEach() 和 .map() 之间的区别

  • While using forEach, only the values containing the data need to be pushed in the final result.使用 forEach 时,只需要将包含数据的值推送到最终结果中。 So, we need to put length check for > 0.因此,我们需要对 > 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.如有任何疑问,请回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM