Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. ["Teacher 1", "Student 1", "Student 2", "Student 3", "Student 4", "Student 5"]
  2. ["Teacher 2", "Student 1", "Student 2", "Student 3", "Student 4", "Student 5"]
  3.  
  4. Var Teacher1 = new Teacher("Teacher1");
  5. Teacher1.students = [Student1, Student2, Student3, Student4, Student5];
  6.  
  7. // map takes an array and returns and modifies each object, passed
  8. // as the parameter d
  9. rows.map(function(d){
  10. return {
  11. teacher: d[0],
  12. students: d.slice(1)
  13. };
  14.  
  15. });
  16.  
  17. [
  18. {teacher: 'Teacher 1', students: ['Student 1', 'Student 2',...']},
  19. ]
  20.  
  21. function Teacher(name){
  22. this.name = name;
  23. }
  24.  
  25. var teacher = new Teacher('teacher 1')
  26.  
  27. var teacher = {}; //an empty object
  28.  
  29. teacher.name = 'teacher 1';
  30. teacher.students = ['student 1', ...]
  31.  
  32. var teacher = {
  33. name: 'teacher 1',
  34. students: ['student 1',' student 2',...']
  35. };
  36.  
  37. function example(item) {
  38. // where item is an array
  39. return {
  40. name: item[0],
  41. students: item.slice(1) // takes all the array elements after 0
  42. };
  43. }
  44.  
  45. function Teacher(name, students) {
  46. this.name = name;
  47. this.students = students;
  48. }
  49.  
  50. rows.map(function(d){
  51. return new Teacher(d[0], d.slice(1));
  52. });
  53.  
  54. var myProcessedCSV = [
  55. ["Teacher 1", "Student 1", "Student 2", "Student 3", "Student 4", "Student 5"]
  56. ["Teacher 2", "Student 1", "Student 2", "Student 3", "Student 4", "Student 5"]
  57. ]
  58.  
  59. // start the processing
  60. myProcessedCSV.forEach(processCSVRow);
  61.  
  62. // this function takes a single row and instantiates a Teacher object
  63. // with it
  64. function processCSVRow(row){
  65. // this extracts the teacher's name (the first data in the row)
  66. var teacherName = row.shift();
  67. var teacher = new Teacher(teacherName);
  68. // all the remaining data points in the row are students, so we
  69. // can simply use 'row' here
  70. teacher.students = row;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement