Here's how I declare my array of objects:
let arrayOfObjects = [{ _id:0, names:'' }];
And I want to assign the values like below:
for (var i = 0; i < kelasList.length; i++){
for (var j = 0; j < absentees[i].length; j++){
arrayOfObjects[i]._id = absentees[i][j]._id
arrayOfObjects[i].names = absentees[i][j].firstName + " " + absentees[i][j].lastName
}
}
Running the code above will return
UnhandledPromiseRejectionWarning: TypeError: Cannot set property '_id' of undefined at line
which basically points to the line
arrayOfObjects[i]._id
But it will assign the value without any problem, if I write
arrayOfObjects[0]._id
instead of
arrayOfObjects[i]._id
My assumption was let arrayOfObjects = [{ _id:0, names:'' }]; would create an array of objects, so I would be able to access/set its values with
arrayOfObjects[i]._id
but it seems let arrayOfObjects = [{ _id:0, names:'' }]; here will only create a single array of object. So currently for me to pass more values, I will need to declare it something like
let arrayOfObjects = [{ _id:0, names:'' },{ _id:0, names:'' },{ _id:0, names:'' },{ _id:0, names:'' },{ _id:0, names:'' }];
But of course, this isn't feasible because I don't know what should my array size be. So actually how should I declare the array of objects that can contain dynamic size of objects?