Reputation: 741
I am stuck with a real stupid problem. I trying to use D3.js to plot data(some scores of people) dynamically.
I am stuck since 3 hours, believe or not.
If somebody can help me. Thanks
var data = [];
record = {Fanny : 40, Joe : 20};
var time = 0;
record["time"] = time;
console.log(record);
myTimer = setInterval(function () {
time = time + 1;
record.time = time;
console.log(record);
data.push(record);
for (i = 0; i < data.length; i++) {
console.log(data[i].time)
}
},1000);
Upvotes: 0
Views: 100
Reputation: 510
You may be dealing with a referencing issue, meaning that say
a is an object
a.name = 'toto';
var b = a;
b.name = 'titi';
console.log(a.name) // will output 'titi'
One way to work around that is to do the not so pretty
var b = JSON.parse(JSON.stringify(a));
to break the referencing
Upvotes: 0
Reputation: 262514
You need to make a new record
for every iteration.
Right now, you have just the same single (but repeated many times in the array) record
and keep updating it.
data.push({ Fanny: 40, Joe: 20, time: time });
Upvotes: 2