2

Here's what I've got. One array of objects:

var teachers = [{
               Year: 2016,
               FullName: "Matt",
               Age: 39
             },
             {
               Year: 2016,
               FullName: "Sara",
               Age: 25
             },
             ...
            ];

And another array of objects. These would be nested like so:

var students = [[
                  {
                    Year: 2016,
                    FullName: "Zoe"
                    Age: 8
                  }
                ],
                [
                  {
                    Year: 2016,
                    FullName: "Lulu"
                    Age: 9
                  },
                  {
                    Year: 2016,
                    FullName: "Leo",
                    Age: 13
                  }
                ],
                [ // empty array here
                ],
                [
                  {
                    Year: 2016,
                    FullName: "Lotta",
                    Age: 11
                  }
                ]
                ...
               ];

How they are organized is that students[0] is a student of teachers[0]. students[4] are students of teachers[4], and so forth.

What I was attempting to do what to take the FullName property, 'Students' in each student and put those values into an array of a new property of teachers called 'SundayStudents'. So what I'd end up with would be:

teachers = [{
             Year: 2016,
             FullName: "Matt",
             Age: 39,
             SundayStudents: ["Zoe"]
            },
            {
              Year: 2016,
              FullName: "Sara",
              Age: 25,
              SundayStudents: ["Lulu", "Leo"]
            },
             ...
          ];

I tried a nested for-loop, but the students array has varying numbers of objects in each sub-array, and it doesn't create an array for the new property. I think I'm stuck.

  for (var j = 0, leng = teachers.length; j < leng; j++) {
    for (var k = 0, lent = students.length; k < lent; k++)
      Teachers[i].SundayStudents = Students[j][k].FullName;
  }

Any hints are welcome.

1
  • You can use .push() to append to an array. Commented Jun 20, 2016 at 13:55

8 Answers 8

1

You could just iterate and check if the target element exist. Then you could make a new property with the mapped names.

var teachers = [{ Year: 2016, FullName: "Matt", Age: 39 }, { Year: 2016, FullName: "Sara", Age: 25 }],
    students = [[{ Year: 2016, FullName: "Zoe", Age: 8 }], [{ Year: 2016, FullName: "Lulu", Age: 9 }, { Year: 2016, FullName: "Leo", Age: 13 }], [{ Year: 2016, FullName: "Lotta", Age: 11 }]];

students.forEach(function (a, i) {
    if (Array.isArray(a) && teachers[i]) {
        teachers[i].SundayStudents = a.map(function (b) {
            return b.FullName;
        });
    }
});

console.log(teachers);

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate the use of map. Thanks for your time.
1

Don't overdo the loops. You can use the outer loop's index for both teachers and student arrays.

for (var i = 0; i < teachers.length; i++) {
  teachers[i].SundayStudents = []
  for (var j = 0; j < students[i].length; j++) {
    teachers[i].SundayStudents.push(students[i][j].FullName);
  }
}

Comments

1

well, if all students' elements are arrays (a 2-dimensional array) then you just need to add one more for loop to scrape the inner array

teachers[i].SundayStudents = [];
for (var j = 0; j < teachers.length; j++) {
    for (var k = 0; k < students.length; k++) {
        var studentSub = students[k];
        for (var l = 0; l < studentSub.length; l++) {
            teachers[j].SundayStudents.push(students[k][l].FullName);
        }
    }
}

Comments

1

You are using students.length in the inner for loop which should have been students[j].length because it is an array of array.

var teachers = [{
               Year: 2016,
               FullName: "Matt",
               Age: 39
             },
             {
               Year: 2016,
               FullName: "Sara",
               Age: 25
             }
            ]

var students = [[
                  {
                    Year: 2016,
                    FullName: "Zoe",
                    Age: 8
                  }
                ],
                [
                  {
                    Year: 2016,
                    FullName: "Lulu",
                    Age: 9
                  },
                  {
                    Year: 2016,
                    FullName: "Leo",
                    Age: 13
                  }
                ],
                [ // empty array here
                ],
                [
                  {
                    Year: 2016,
                    FullName: "Lotta",
                    Age: 11
                  }
                ]
                ]

for(var i = 0; i < teachers.length; i++){
    teachers[i].SundayStudents = []

    for(var j = 0; j < students[i].length; j++){
        teachers[i].SundayStudents[j] = students[i][j].FullName
    }
}

console.log(teachers)

Comments

1

The solution using Array.forEach and Array.map functions:

teachers.forEach(function(v, i, arr) { // arr - the array that forEach() is being applied to
    if (Array.isArray(students[i]) && students[i].length) { // check for non-empty array
        arr[i].SundayStudents = students[i].map((st) => st.FullName);
    }        
});

console.log(JSON.stringify(teachers, 0, 4));

The output:

[
    {
        "Year": 2016,
        "FullName": "Matt",
        "Age": 39,
        "SundayStudents": [
            "Zoe"
        ]
    },
    {
        "Year": 2016,
        "FullName": "Sara",
        "Age": 25,
        "SundayStudents": [
            "Lulu",
            "Leo"
        ]
    }
    ...
]

Comments

1

Loop through teachers. Copy the object. Add SundayStudents as array. Loop through students of the corresponding index and push the FullName to SundayStudents :

var teachers = [{
  Year: 2016,
  FullName: "Matt",
  Age: 39
}, {
  Year: 2016,
  FullName: "Sara",
  Age: 25
}];

var students = [
  [{
    Year: 2016,
    FullName: "Zoe",
    Age: 8
  }],
  [{
    Year: 2016,
    FullName: "Lulu",
    Age: 9
  }, {
    Year: 2016,
    FullName: "Leo",
    Age: 13
  }]
];

var r = [];

teachers.forEach(function(obj, i) {
  var o = {};
  o = obj;
  o.SundayStudents = [];
  students[i].forEach(function(d) {
    o.SundayStudents.push(d.FullName);
  });
  r.push(o);
});

console.log(r);

1 Comment

Huge thanks. Your answer helped understand what's going on.
1

This appears to be a simple job of maps.

var students = [[
                  {
                    Year: 2016,
                    FullName: "Zoe",
                    Age: 8
                  }
                ],
                [
                  {
                    Year: 2016,
                    FullName: "Lulu",
                    Age: 9
                  },
                  {
                    Year: 2016,
                    FullName: "Leo",
                    Age: 13
                  }
                ],
                [ // empty array here
                ],
                [
                  {
                    Year: 2016,
                    FullName: "Lotta",
                    Age: 11
                  }
                ]],
teachers = [{
             Year: 2016,
             FullName: "Matt",
             Age: 39
            },
            {
              Year: 2016,
              FullName: "Sara",
              Age: 25
            },
            {
             Year: 2016,
             FullName: "Yellow Beard",
             Age: 39
            },
            {
              Year: 2016,
              FullName: "Professor Oclitus",
              Age: 25
            },
          ];
          
teachers = teachers.map((t,i) => (t.SundayStudents = students[i].map(s => s.FullName),t));
console.log(teachers);

Wow.. i just noticed there was an answer exactly the same. So let me give an ES5 compatible version.

teachers = teachers.map(function(t,i) {
                                        t.SundayStudents = students[i].map(function(s) {
                                                                                         return s.FullName;
                                                                                       });
                                        return t;
                                      });

1 Comment

Very funky indentation style.
0

Just because it's possible, here's a one line answer

teachers.map((t, i) => (t.sundayStudents = students[i].map(s => s.FullName), t));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.