Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var students = [];
function addStudent(student) {
  // Check if we already know about this student.
  var existingRecord = students.find(function (s) {
    return s.student_id === student.student_id;
  });
  
  var classInfo = {
    class_number: student.class_number,
    location: student.location
  };
  
  if (!existingRecord) {
    // This is the first record for this student so we construct
    // the complete record and add it.
    students.push({
      student_id: student.student_id,
      classes: [classInfo]
    });
    
    return;
  }
  
  // Add to the existing student's classes.
  existingRecord.classes.push(classInfo);
}
addStudent({
    "student_id": "12345",
    "class_number": "abcd",
    "location": "below",
});
addStudent({
    "student_id": "12345",
    "class_number": "efgh",
    "location": "up",
});
addStudent({
    "student_id": "67890",
    "class_number": "abcd",
    "location": "below",
});
console.log(JSON.stringify(students, null, 2));
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers