Array.push() makes all elements the same when pushing an object
Cloning the object before pushing into the array, also solves the problem.
temp = clone(s);
subscribers.push(temp);
Get https://www.npmjs.com/package/clone
The problem is not with the push
method of the Array.prototype
but with your bindings.
You are modifying the same s
object in every iteration in your async.foreach
block which is actually the same Object as the previously defined Subscriber
.
First you should move the declaration of the s
variable to the foreach block.
And also if you want to create an object with default values, it should be a function
, which returns a new object:
function Subscriber() {
return {
'userID': '',
'email': '',
'name': '',
'stage': '',
'poster': false,
'canEmail': false,
'stage': ''
};
};
And then you can instantiate a Subscriber
object like this:
var s = Subscriber();
See this answer or Closures on MDN for more explanation.