【问题标题】:Underscore groupBy of nested object elements嵌套对象元素的下划线 groupBy
【发布时间】:2017-05-22 14:58:05
【问题描述】:

我有一些嵌套对象的数据需要使用 underscoreJS 对嵌套对象进行分组。

[
      {
        "_id": "58e28acba878a54951c84e36",
        "InId": "5809c226e41a193f4ffc1fa5",
        "StuID": "58185aacd7e86244252eb3f0",
        "ExamList": {
          "Type": "FinalMk",
          "SchdlId": "58e28acba878a54951c84dd0",
          "Exams": {
            "SubjId": "5825803ec4a0aff62070d9f5",
            "MaxMark": 100,
            "PassMark": 50,
            "ExDate": "2016-08-11T18:30:00.000Z",
            "Mark": 62
          }
        }
      },
      {
        "_id": "58e28acba878a54951c84e36",
        "InId": "5809c226e41a193f4ffc1fa5",
        "StuID": "58185aacd7e86244252eb3f0",
        "ExamList": {
          "Type": "FinalMk",
          "SchdlId": "58e28acba878a54951c84dd0",
          "Exams": {
            "SubjId": "5825803ec4a0aff62070d9f6",
            "MaxMark": 100,
            "PassMark": 50,
            "ExDate": "2016-08-09T18:30:00.000Z",
            "Mark": 76
          }
        }
      },
    ],
    [
      {
        "_id": "58e28acba878a54951c84e4d",
        "InId": "5809c226e41a193f4ffc1fa5",
        "StuID": "58185aacd7e86244252eb3ec",
        "ExamList": {
          "Type": "FinalMk",
          "SchdlId": "58e28acba878a54951c84dd0",
          "Exams": {
            "SubjId": "5825803ec4a0aff62070d9f5",
            "MaxMark": 100,
            "PassMark": 50,
            "ExDate": "2016-08-11T18:30:00.000Z",
            "Mark": 59
          }
        }
      },
      {
        "_id": "58e28acba878a54951c84e4d",
        "InId": "5809c226e41a193f4ffc1fa5",
        "StuID": "58185aacd7e86244252eb3ec",
        "ExamList": {
          "Type": "FinalMk",
          "SchdlId": "58e28acba878a54951c84dd0",
          "Exams": {
            "SubjId": "5825803ec4a0aff62070d9f6",
            "MaxMark": 100,
            "PassMark": 50,
            "ExDate": "2016-08-09T18:30:00.000Z",
            "Mark": 56
          }
        }
      },
    ],

这里我必须使用“SubjId”对嵌套对象进行分组

我已经尝试使用这些代码 bt 我们的输入是未定义的“考试”。

var groupBySubj = _.groupBy(docs.ExamList.Exams, 'SubjId');

【问题讨论】:

  • 在您提供的数据中,数组在 sn-p 的中间终止...您可以编辑并修复它以使您的问题更清晰吗?

标签: javascript underscore.js


【解决方案1】:

要按嵌套属性分组,可以使用函数:

_.groupBy(data, function(student) { return student.ExamList.Exams.SubjId; })

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 2016-05-25
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    相关资源
    最近更新 更多