title: combining items in a json
body: <p>I was able to write a solution, but I was wondering if there is a more elegant and efficient way to do it.</p>
  
  <pre><code>    var arr = [];
  
  //I have a array like this:
      arr = [{
          teacherid: 10,
          teacherName: 'andy',
          age: 30,
          studentid: 1,
          studentName: 'A',
          studentAge: 10
        }, 
        {
          teacherid: 10,
          teacherName: 'andy',
          age: 30,
          studentid: 2,
          studentName: 'B',
          studentAge: 11
        }, {
          teacherid: 10,
          teacherName: 'andy',
          age: 30,
          studentid: 3,
          studentName: 'C',
          studentAge: 12
        },
  
        {
          teacherid: 11,
          teacherName: 'May',
          age: 40,
          studentid: 4,
          studentName: 'D',
          studentAge: 9
        }, {
          teacherid: 11,
          teacherName: 'May',
          age: 40,
          studentid: 5,
          studentName: 'E',
          studentAge: 12
        }
      ];
  
  
  //want to group the students info in each indiviual teacher like this:
  //*
      [{
        "teacherid": 10,
        "teacherName": "andy",
        "age": 30,
        "studentArr": [{
          "studentid": 1,
          "studentName": "A",
          "studentAge": 10
        }, {
          "studentid": 2,
          "studentName": "B",
          "studentAge": 11
        }, {
          "studentid": 3,
          "studentName": "C",
          "studentAge": 12
        }]
      }, {
        "teacherid": 11,
        "teacherName": "May",
        "age": 40,
        "studentArr": [{
          "studentid": 4,
          "studentName": "D",
          "studentAge": 9
        }, {
          "studentid": 5,
          "studentName": "E",
          "studentAge": 12
        }]
      }
      }]
  *//
  
  
  
  //my solution
      var result = [];
      var first = true;
      arr = _.sortBy(arr, 'teacherid');
  
  
      (function() {
        for (var i = 0; i &lt; arr.length; i++) {
          for (var j = i; j &lt; arr.length; j++) {
            console.log("comparing i:" + i + " with j:" + j);
  
            if (i === j &amp;&amp; j === arr.length) {
              console.log("exit " + i + " : " + j)
              return;
            } else if (arr[i].teacherid === arr[j].teacherid) {
              if (first) {
                result.push({
                  teacherid: arr[j].teacherid,
                  teacherName: arr[j].teacherName,
                  age: arr[j].age,
                  studentArr: [{
                    studentid: arr[j].studentid,
                    studentName: arr[j].studentName,
                    studentAge: arr[j].studentAge
                  }]
                })
              } else {
                //find in result arr, the obj that contain this teacher and push the student in the array
                var teacher = _.find(result, function(item) {
                  return (item.teacherid === arr[j].teacherid)
                })
                teacher.studentArr.push({
                  studentid: arr[j].studentid,
                  studentName: arr[j].studentName,
                  studentAge: arr[j].studentAge
                })
              }
              first = false;
  
            } else {
              first = true;
              i = j - 1;
              j = arr.length; //end inner loop, go back to outer loop and check next teacherID
            }
          }
        }
  
      })()
  
  
      console.log(JSON.stringify(result));
  </code></pre>
  
  <p>This would be a headache if another programmer were to read it. I was wondering if there is a better solution (more efficient and/or more readable) than this. I feel like I brute-forced this</p>