0

I'm trying to return some values from a list that added some objects. Inside the loop, I'm getting the right output, but outside it, it's not working as it should. I might be failing in something basic, but I really can't find what it's going on. Here's what I have:

  var register = { name: "", number: "" };
  var listName = new Array();
  var listNumber = new Array();
  var listObject = new Array();

  listName.push("A");
  listName.push("B");
  listName.push("C");
  listNumber.push(1);
  listNumber.push(2);
  listNumber.push(3);

  for(var i = 0; i < listName.length; i++)
  {
     register.name = listName[i];
     register.number = listNumber[i];
     listObject.push(register);
     console.log(listObject[i].name) //"A" then "B" then "C"
     console.log(listObject[i].number) //"1" then "2" then "3"
  }

  console.log(listObject[0].name); //"C"
  console.log(listObject[1].name); //"C"
  console.log(listObject[2].name); //"C"
  console.log(listObject[0].number); //"3"
  console.log(listObject[1].number); //"3"
  console.log(listObject[2].number); //"3"

Thanks in advance for any further help!

2
  • 3
    There is only ever one register variable that gets mutated each iteration and pushed three times. Commented Aug 23, 2018 at 1:35
  • Are you aware that you can build arrays like ["A", "B", "C"] and [1, 2, 3], and even [{name: "A", number: 1}, {name: "B", number: 2}, {name: "C", number: 3}]? Commented Aug 23, 2018 at 2:20

3 Answers 3

1

@CertainPerformance is correct. Your lack of understanding of object references in JavaScript is causing this error.

register is declared as a variable outside your loop, so its scope is outside the loop. This means that every time your loop code runs, you reset the properties on the register object, and then you push the same object into the array. So in your example, the listObject array is made up of 3 identical references to the register object.

Change your loop to:

for(let i = 0; i < listName.length; i++) {
  let register = {name: listName[i], number: listNumber[i]};

  listObject.push(register);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for replying!
1

As said before, you have only one register variable which gets overriden every iteration.

You can push directly an object into listObject, without needing to declare an outer scope variable.

  var listName = new Array();
  var listNumber = new Array();
  var listObject = new Array();

  listName.push("A");
  listName.push("B");
  listName.push("C");
  listNumber.push(1);
  listNumber.push(2);
  listNumber.push(3);

  for(var i = 0; i < listName.length; i++)
  {
     listObject.push({ name: listName[i], number: listNumber[i] });
     console.log(listObject[i].name) //"A" then "B" then "C"
     console.log(listObject[i].number) //"1" then "2" then "3"
  }

  console.log(listObject[0].name); //"now A"
  console.log(listObject[1].name); //"now B"
  console.log(listObject[2].name); //"now C"
  console.log(listObject[0].number); //"now 1"
  console.log(listObject[1].number); //"now 2"
  console.log(listObject[2].number); //"now 3"

1 Comment

Thanks for replying!
1

As the function iterates it adds a new register object. Each iteration the register objects keys and values are being updated.

Instead, create a register object each iteration and push it.

Like this:

for(var i = 0; i < listName.length; i++)
  {
     let registerObject = {name: listName[i], number: listNumber[i]};
     listObject.push(registerObject);
     console.log(listObject[i].name) //"A" then "B" then "C"
     console.log(listObject[i].number) //"1" then "2" then "3"
  }

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.