I have the following code. Essentially what I do is I have a loop, and within the loop I have logic that creates a few values. I want to insert those values into an Object, and then insert that Object into an Array.
Now in each iteration, the object values
change, so in case this loop runs 2 times, I'd expect to have two different objects.
Problem is, on first iteration, I push a distinctive object into an array, and then on the 2nd iteration, I have a new object with different values, but after I do .push - it overwrites my earlier entry with the new values, and insert a new object into the array.
I.E. I end up have 2 identical objects in the array. What is happening?
var arr = [];
var filObj={};
for (var i = 0; i<3; i++){ //looping
//I do some logic and derive a few fields and insert them into my `filObj`
filObj['col1'] = 'Hello';
filObj['col2'] = 'Yellow';
filObj['col3'] = 'Seven';
arr.push(filObj);
}