0

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?

3 Answers 3

1

You could simply check if the element already exists in the array and if it doesn't, create it.

let arrayOfObjects = [];

for (var i = 0; i < kelasList.length; i++) {
  for (var j = 0; j < absentees[i].length; j++) {
    if (!arrayOfObjects[i]) {
      arrayOfObjects[i] = {
        _id: 0,
        names: ''
      };
    }
    arrayOfObjects[i]._id = absentees[i][j]._id
    arrayOfObjects[i].names = absentees[i][j].firstName + " " + absentees[i][j].lastName
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

By setting arrayOfObjects[i]._id, you are setting the attribute _id on an already existing object arrayOfObjects[i]. But for i >= 1, this object doesn't exist.

Instead, you need to set the whole object in the loop:

for (var i = 0; i < kelasList.length; i++){
  for (var j = 0; j < absentees[i].length; j++){                    
    arrayOfObjects[i] = {
        _id: absentees[i][j]._id, 
        names: absentees[i][j].firstName + " " + absentees[i][j].lastName}
    }
}

This will work just fine, but will replace the whole object at the index i. If instead you want to set the _id and names attribute of an object that could already be existing with different keys (say, address), you would check if the object exists:

for (var i = 0; i < kelasList.length; i++){
  for (var j = 0; j < absentees[i].length; j++){ 
    let id = absentees[i][j]._id;
    let names = arrayOfObjects[i].names = absentees[i][j].firstName + " " + absentees[i][j].lastName; 

    if(arrayOfObjets[i]){
      arrayOfObjects[i]._id = id;
      arrayOfObjects[i].names = names;
    }else{         
      arrayOfObjects[i] = {
        _id: absentees[i][j]._id, 
        names: absentees[i][j].firstName + " " + absentees[i][j].lastName
      }
    }
  }
}

There is one last problem with your code. You are iterating over i, then over j, and are setting arrayOfObjects[i]. This means that your double loop is basically the same thing as:

for (var i = 0; i < kelasList.length; i++){
  let lastAbsentee = absentees[i][absentees[i].length-1];
  arrayOfObjects[i]._id = lastAbsentee._id
  arrayOfObjects[i].names = lastAbsentee.firstName + " " + lastAbsentee.lastName
}

Are you sure that is what you want to do? Somehow I would be doubting it.

1 Comment

I would also address the odd iterating over kelasList which just seems to be absentees with extra steps.
1
  • Your arrayOfObjects has a size of 1 when you initialize it, but then you look into arrayOfObjects[i] after i === 0 which cause the undefined behavior since there are no items there (yet).
  • In your for loop, you're also overwriting on arrayOfObjects[i] multiple times (j times to for precision).

Try using this

let arrayOfObjects = [];

for (var i = 0; i < absentees.length; i++) {
    for (var j = 0; j < absentees[i].length; j++) {
        arrayOfObjects.push({
            _id: absentees[i][[j]._id,
            names: absentees[i][j].firstName + " " + absentees[i][j].lastName
        });
    }
}

NOTE: The iteration logic may be subject to change since I didn't really get your intention from your code. The "adding items to the arrray" part is good though.

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.