通过StudentId对数组进行分组,并找到具有相同元素的子数组的总和

时间:2018-03-04 17:43:30

标签: javascript arrays json

 [
    {
        "id": 1,
        "subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
        "present": "false",
        "date": "2018-03-04",
        "createdAt": "2018-03-04T14:06:18.000Z",
        "updatedAt": "2018-03-04T14:06:18.000Z",
        "SemesterId": 1,
        "BatchId": 1,
        "StudentId": 1,
        "Student": {
            "id": 1,
            "rollnumber": 11111,
            "studentname": "LAWANNA HAUGHT",
            "dateofbirth": "1997-1-01",
            "mobilenumber": "9875449112",
            "createdAt": "2018-03-03T14:59:01.000Z",
            "updatedAt": "2018-03-03T14:59:01.000Z",
            "BatchId": 1
          }
    },
    {
        "id": 7,
        "subject": "COMPUTER ORGANIZATION",
        "present": "true",
        "date": "2018-03-04",
        "createdAt": "2018-03-04T14:09:12.000Z",
        "updatedAt": "2018-03-04T14:09:12.000Z",
        "SemesterId": 1,
        "BatchId": 1,
        "StudentId": 1,
        "Student": {
            "id": 1,
            "rollnumber": 11111,
            "studentname": "LAWANNA HAUGHT",
            "dateofbirth": "1997-1-01",
            "mobilenumber": "9875449112",
            "createdAt": "2018-03-03T14:59:01.000Z",
            "updatedAt": "2018-03-03T14:59:01.000Z",
            "BatchId": 1
        }
    },
    {
      "id": 2,
      "subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
      "present": "true",
      "date": "2018-03-04",
      "createdAt": "2018-03-04T14:06:18.000Z",
      "updatedAt": "2018-03-04T14:06:18.000Z",
      "SemesterId": 1,
      "BatchId": 1,
      "StudentId": 2,
      "Student": {
          "id": 2,
          "rollnumber": 11112,
          "studentname": "KAILA HALBROOK",
          "dateofbirth": "1997-1-01",
          "mobilenumber": "9875449113",
          "createdAt": "2018-03-03T14:59:01.000Z",
          "updatedAt": "2018-03-03T14:59:01.000Z",
          "BatchId": 1
      }
    },
    {
        "id": 8,
        "subject": "COMPUTER ORGANIZATION",
        "present": "false",
        "date": "2018-03-04",
        "createdAt": "2018-03-04T14:09:12.000Z",
        "updatedAt": "2018-03-04T14:09:12.000Z",
        "SemesterId": 1,
        "BatchId": 1,
        "StudentId": 2,
        "Student": {
            "id": 2,
            "rollnumber": 11112,
            "studentname": "KAILA HALBROOK",
            "dateofbirth": "1997-1-01",
            "mobilenumber": "9875449113",
            "createdAt": "2018-03-03T14:59:01.000Z",
            "updatedAt": "2018-03-03T14:59:01.000Z",
            "BatchId": 1
        }
    }
]

这是一批的出勤记录。 我正在尝试将此数组转换为以下形式。

具有特定StudentId的所有记录必须与一个StudentId和studentname一起使用。

[
    {
        "StudentId": 1,
        "studentname": "LAWANNA HAUGHT",
        "rollnumber": 11111,
        "attendance": [
          {
              "subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
              "present": 0,
              "absent": 1,
              "total": 1
          }, 
          {
              "subject": "COMPUTER ORGANIZATION",
              "present": 1,
              "absent": 0,
              "total": 1
          }
        ]
    }, 
    {
        "StudentId": 2,
        "studentname": "KAILA HALBROOK",
        "rollnumber": 11111,
        "attendance": [
          {
              "subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
              "present": 1,
              "absent": 0,
              "total": 1
          }, 
          {
              "subject": "COMPUTER ORGANIZATION",
              "present": 0,
              "absent": 1,
              "total": 1
          }
        ]
    }
]

present是具有相同StudentId和subjectname且present = true

的行数

缺少具有相同StudentId和subjectname且present = false

的行数

total是具有相同StudentId和subjectname的行数。

这是我在一个StackOverflow答案和使用underscore.js的帮助下编写的代码。 但它找到了所有人的现有,缺席和总价值,而不是学生和主题。

var groups = _.groupBy(list, 'StudentId');
var result = _.map(groups, function(student){
    return {
        StudentId: student[0].StudentId,
        studentname: student[0].Student.studentname,
        rollnumber: student[0].Student.rollnumber,
        attendances: _.map(student, function(item){
            return {
              subject:item.subject,
              present: _.reduce(item, function(memo, att){
                        if(att.present=='true')
                          return memo + 1;
                        else
                          return memo;
                      }, 0),
              absent: _.reduce(item, function(memo, att){
                        if(att.present=='false')
                          return memo + 1;
                        else
                          return memo;
                      }, 0),
              total: _.reduce(item, function(memo, att){
                          return memo + 1;
                      }, 0)
            }
        })
      })
    }
  }
);

2 个答案:

答案 0 :(得分:1)

好的,这有点奇怪了。如果您正在编写后端编码,那么在查询端处理此问题可能是有意义的。话虽这么说,你可以使用reduce to" collapse"将数组转换为基于StudentId的对象。在此过程中,您可以构建记录的外观形状。我并不认为这是使用reduce的标准方法,因为您正在转换数组。话虽如此,我无法想到使用地图的简单方法。这个操作的结果给了我们一个对象,我们可以使用Object.values()将它返回到一个数组。我不了解现在,缺席和总数的用例。

这是JSFiddle

function isolateRecords(data) {
    return Object.values(data.reduce((accum, current) => {
        let attenance = {
            subject: current.subject,
            present: current.present === "true" ? 1 : 0,
            absent: current.present === "false" ? 1 : 0,
            total: 0
        };
        attenance.total = attenance.present + attenance.absent;
        if (accum[current.StudentId]) {
            accum[current.StudentId].attendance.push(attenance);
        } else {
            accum[current.StudentId] = {
            StudentId: current.StudentId,
            studentname: current.Student.studentname,
            rollnumber: current.Student.rollnumber,
            attendance: [attenance]
            }
        }
        return accum;
    }, {}));
}
isolateRecords(myOriginalArrayOfRecords);

答案 1 :(得分:1)

您可以通过寻找strunden以及稍后寻找出席来使用嵌套方法。



Eq

var data = [{ id: 1, subject: "FUNDAMENTALS OF INFORMATION TECHNOLOGY", present: "false", date: "2018-03-04", createdAt: "2018-03-04T14:06:18.000Z", updatedAt: "2018-03-04T14:06:18.000Z", SemesterId: 1, BatchId: 1, StudentId: 1, Student: { id: 1, rollnumber: 11111, studentname: "LAWANNA HAUGHT", dateofbirth: "1997-1-01", mobilenumber: "9875449112", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }, { id: 7, subject: "COMPUTER ORGANIZATION", present: "true", date: "2018-03-04", createdAt: "2018-03-04T14:09:12.000Z", updatedAt: "2018-03-04T14:09:12.000Z", SemesterId: 1, BatchId: 1, StudentId: 1, Student: { id: 1, rollnumber: 11111, studentname: "LAWANNA HAUGHT", dateofbirth: "1997-1-01", mobilenumber: "9875449112", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }, { id: 2, subject: "FUNDAMENTALS OF INFORMATION TECHNOLOGY", present: "true", date: "2018-03-04", createdAt: "2018-03-04T14:06:18.000Z", updatedAt: "2018-03-04T14:06:18.000Z", SemesterId: 1, BatchId: 1, StudentId: 2, Student: { id: 2, rollnumber: 11112, studentname: "KAILA HALBROOK", dateofbirth: "1997-1-01", mobilenumber: "9875449113", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }, { id: 8, subject: "COMPUTER ORGANIZATION", present: "false", date: "2018-03-04", createdAt: "2018-03-04T14:09:12.000Z", updatedAt: "2018-03-04T14:09:12.000Z", SemesterId: 1, BatchId: 1, StudentId: 2, Student: { id: 2, rollnumber: 11112, studentname: "KAILA HALBROOK", dateofbirth: "1997-1-01", mobilenumber: "9875449113", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }],
    grouped = [];

data.forEach(({ subject, present, StudentId, Student: { rollnumber, studentname } }) => {
    var student = grouped.find(g => StudentId === g.StudentId),
        attendance;

    if (!student) {
        student = { StudentId, studentname, rollnumber, attendance: [] };
        grouped.push(student);
    }

    attendance = student.attendance.find(a => a.subject === subject);
    if (!attendance) {
        attendance = { subject, present: 0, absent: 0, total: 0 };
        student.attendance.push(attendance);
    }
    attendance[{ true: 'present', false: 'absent' }[present]]++;
    attendance.total++;
});

console.log(grouped);