Array.Push覆盖之前的Value Node Js
Array.Push overwrites the previous Value Node Js
美好的一天。
我有一个全局数组,它必须是全局的。
var comments= [];
我有一个套接字的回调,我正在迭代它并添加值。
更好的代码:
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);
});
我的 console.Log("data added with id)...
正在正确打印所有内容,理想情况下我想向 现有数据添加一个新值,即 json 数据 和新值的名称是 eachUserId
,正如我在循环中所做的那样,该值必须完全不同,如您所见。
这是我之后如何获得物品的。
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);
}
}
此处 eachUserId 始终是循环中添加的最后一项...我做错了什么?为什么 push()
方法会覆盖每个值?
您确定您的 comments
包含所需数据吗?将它包裹在 closure
中,因此它会创建一个 new scope for each iteration
并传递正确的数据。
for (var f = 0; f < Object.keys(comments).length; f++) {
(function(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);
}
)(f))
}
问题:
您的问题是当您将 eachUserId
的值分配给对象 data
时。您只有 1 个名为 data
的对象,并且您一遍又一遍地将同一个对象添加到数组中。但请记住,它们都引用同一个对象,当您更改该对象中的任何内容时,它会反映到所有地方。
因此,当您在循环中更改 data.eachUserId = key;
时,它会更改数组中的所有项目。最后,它们都包含您分配给它的最后一个 eachUserId
值。
解法:
您需要克隆对象,然后将其推入数组。
我建议您使用 lodash library and cloneDeep 方法创建对象的深度克隆。
var _ = require('lodash'); //require "lodash"
socket.on('commentAdded', function (data) {
if (data !== null) {
Object.keys(users).forEach(function (key) {
if (key !== "null") {
var dataClone = _.cloneDeep(data); //create deep clone
dataClone.eachUserId = key; //assign "key"
comments.push(dataClone); //push into the array
}
});
console.log("comment was added");
}
socket.broadcast.emit('onCommentAdded', data);
});
使用Spread Operator,已经是Stage 4(ECMAScript)
const foo = {}
const bar = {...foo}
foo.a = 1;
bar.a = 10;
console.log(foo) // {a : 1}
console.log(bar) // {a : 10}
美好的一天。 我有一个全局数组,它必须是全局的。
var comments= [];
我有一个套接字的回调,我正在迭代它并添加值。
更好的代码:
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);
});
我的 console.Log("data added with id)...
正在正确打印所有内容,理想情况下我想向 现有数据添加一个新值,即 json 数据 和新值的名称是 eachUserId
,正如我在循环中所做的那样,该值必须完全不同,如您所见。
这是我之后如何获得物品的。
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);
}
}
此处 eachUserId 始终是循环中添加的最后一项...我做错了什么?为什么 push()
方法会覆盖每个值?
您确定您的 comments
包含所需数据吗?将它包裹在 closure
中,因此它会创建一个 new scope for each iteration
并传递正确的数据。
for (var f = 0; f < Object.keys(comments).length; f++) {
(function(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);
}
)(f))
}
问题:
您的问题是当您将 eachUserId
的值分配给对象 data
时。您只有 1 个名为 data
的对象,并且您一遍又一遍地将同一个对象添加到数组中。但请记住,它们都引用同一个对象,当您更改该对象中的任何内容时,它会反映到所有地方。
因此,当您在循环中更改 data.eachUserId = key;
时,它会更改数组中的所有项目。最后,它们都包含您分配给它的最后一个 eachUserId
值。
解法:
您需要克隆对象,然后将其推入数组。
我建议您使用 lodash library and cloneDeep 方法创建对象的深度克隆。
var _ = require('lodash'); //require "lodash"
socket.on('commentAdded', function (data) {
if (data !== null) {
Object.keys(users).forEach(function (key) {
if (key !== "null") {
var dataClone = _.cloneDeep(data); //create deep clone
dataClone.eachUserId = key; //assign "key"
comments.push(dataClone); //push into the array
}
});
console.log("comment was added");
}
socket.broadcast.emit('onCommentAdded', data);
});
使用Spread Operator,已经是Stage 4(ECMAScript)
const foo = {}
const bar = {...foo}
foo.a = 1;
bar.a = 10;
console.log(foo) // {a : 1}
console.log(bar) // {a : 10}