Good day. I have an global array and it MUST be global.
var comments= [];
I have an callback of a socket where i am iterating through it and adding the values.
Better with the code :
socket.on('commentAdded', function (data) {
if (data !== null) {
var stringify = JSON.stringify(data);
var json = JSON.parse(stringify);
Object.keys(users).forEach(function (key) {
if (key !== "null") {
data.eachUserId = key;
console.log("data added with id " + key + " the size of comments is " + comments.size);
comments.push(data);
}
});
console.log("comment was added");
}
socket.broadcast.emit('onCommentAdded', data);
});
Here my console.Log("data added with id)...
is printing everything correctly and ideally i want to add a new value to the existing data which is a json data and the name of a new value is eachUserId
which value must be fully different as i am doing it inside the loop as you can see.
And here is how i get the items afterwards.
for (var f = 0; f < Object.keys(comments).length; f++) {
var comment = comments[f];
var eachUserId = comment.eachUserId;
console.log("checking with current user id" + userId + " each user id" + eachUserId + " or each user id in [] way " + comment['eachUserId']);
if (eachUserId === userId) {
socket.emit('onCommentAdded', comment);
}
}
Here the eachUserId is always the very last item which was added in loop... what am i doing wrong? Why the push()
method overwrite every value?
via Volo Apps
No comments:
Post a Comment