1

Just a bit confused as to why the following is occurring:

let groupdetails = {
      groupName: "",
};    
const groupsArray = [];

groupdetails.groupName = 'A'
groupsArray.push(groupdetails)
groupdetails.groupName = 'B'
groupsArray.push(groupdetails)
console.log(groupsArray)

The result I am getting is:

[ { groupName: "B" }, { "groupName": "B" } ]

The result I was expecting was:

[ { groupName: "A" }, { "groupName": "B" } ]

Unsure what I am doing wrong?

4
  • 1
    You are pushing the same object to the array twice. groupsArray[0] === groupsArray[1]. Create a new object in the middle and you'll get what you want. Commented Jun 22, 2021 at 6:54
  • @CherryDT - could you please assist with what you mean about creating a new object? Commented Jun 22, 2021 at 6:56
  • groupdetails = { groupName: 'B' } instead of groupdetails.groupName = 'B'; or adding groupdetails = { ...groupdetails } before the groupdetails.groupName = 'B' - otherwise you are accessing the same groupdetails that you just pushed and change it (and push the same thing a second time afterwards) - pushing the object won't create a copy, the element in the array points to the same object that your variable also points to at that point Commented Jun 22, 2021 at 6:56
  • Maybe this helps to understand: stackoverflow.com/questions/518000/… Commented Jun 22, 2021 at 6:57

3 Answers 3

1

You should create a different reference each time you push an element

let groupdetails = {
      groupName: "",
};    
const groupsArray = [];

groupdetails.groupName = 'A'
groupsArray.push(groupdetails)
groupdetails = {...groupdetails}
groupdetails.groupName = 'B'
groupsArray.push(groupdetails)
console.log(groupsArray)

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

Comments

1

You are facing issue with Shallow Copy and Deep Copy..

In your case eventhough you were updating the value groupdetails.groupName = 'B', this referes to the same node as the one you pushed initially. You have to creat a new node with spread operater or some thing and update its value and push it again.

In order to achieve your requirement you have to follow something like below mentioned.

let groupdetails = {
  groupName: "",
};
const groupsArray = [];

groupdetails.groupName = 'A'
groupsArray.push(groupdetails);
const newNode = { ...groupdetails };
newNode.groupName = 'B';
groupsArray.push(newNode);
console.log(groupsArray);

2 Comments

What happens if I need to push an unknown number of groupdetails, i.e. C, D, etc ?
@ArthurJ You can either go with this way or can do directly ['A', 'B', 'C'].forEach(node => groupsArray.push({ groupName: node, })};
1

Remember Arrays, Objects are copied by references, not just by values

a = 'John';
b = a;
console.log(a === b);

Here values are copied to another variable.

obj = {name: 'John'};
newObj = obj;
newObj.name = 'Kate';
console.log(newObj);  // {name: 'Kate'}
console.log(obj)      // {name: 'Kate'}

Here the reference is passed to the other object. So once an object is modified it will be reflected on the other object.

In your case, this will work,

const groupsArray = [];

function addNew(value) {
    return { groupName: value}
}

groupsArray.push(addNew('A'));
groupsArray.push(addNew('B'));
console.log(groupsArray);

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.