I have some issues in manipulating an array obtained in the callback function in through websockets
Server side code
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket) {
var currentTime = new Date().getTime();
var startTime = currentTime - (5*60*1000);
step = 30*1000;
var stats = [];
stats["values"] = [1, 2, 3, 4, 3, 4, 5, 8, 2, 3, 4, 1];
stats["start"] = startTime;
stats["end"] = currentTime;
socket.emit('initial', { stats: stats });
socket.on('echo', function (data) {
});
});
Client side code
var data;
socket.on('initial', function (stats) {
data = stats.stats;
});
Now when I try to manipulate the data variable.Like using push and shift
data.values.push(10);
data.values.shift();
I was expecting it to have
2, 3, 4, 3, 4, 5, 8, 2, 3, 4, 1, 10
But I don't know shift operation completely removes all the elements and I get only 10 remaining in the array. What's going wrong?
you will get your response object {stats: stats}
, but you are interested in the stats
property:
var data;
socket.on('initial', function (obj) {
data = obj.stats;
});