In my node js program in routes i have created array named list. I have assigned value to array from function declared in model. Code for route is:
在我的节点js程序中,我创建了名为list的数组。我已经从模型中声明的函数赋值给数组。路线代码是:
var express = require('express');
var router = express.Router();
var questionModel = require('../model/questionModel');
var userModel=require('../model/userModel');
/* GET home page. */
router.get('/', function(req, res, next) {
//declare an array and store the json data
var list=questionModel.getAllQuestions();
for(i=0;i<list.length; i++){
console.log(list[i].submitter);
console.log(userModel.getUserById(list[i].submitter)[0]);
list[i].submitter=userModel.getUserById(list[i].submitter)[0].fname;
}
console.log(list);
//respond with the array
res.json(list);
//res.redirect("../question/" + this_id);
});
module.exports = router;
Here in module i am using local variable as in this class project i am not using any database. My all Models fetch value from one global variable.
在模块中我使用本地变量,因为在这个类项目中,我没有使用任何数据库。我的所有模型都从一个全局变量中获取值。
This code is work fine on first request. But on first request changing value of list[i].submitter locally value of global value change.
此代码在第一次请求时工作正常。但是在第一次请求时改变list [i]的值。发布者本地值的全局值变化。
Change in global value creates problem when i get second request. On second request value return from questionModel.getAllQuestions is unnecessarily updated.
当我得到第二个请求时,全局值的变化会产生问题。在第二个请求值上,来自questionModel.getAllQuestions的值不必要地更新。
1 个解决方案
#1
2
Array is always pass by reference in javascript. That's the reason why when you edit the array it affects the original array.
数组始终在javascript中通过引用传递。这就是为什么在编辑数组时它会影响原始数组的原因。
In your case, though you're making a copy of questionModel, still what your getting is a list of objects in your array. The objects reference in both the arrays will refer to same object.
在您的情况下,虽然您正在制作questionModel的副本,但您获得的仍然是数组中的对象列表。两个数组中的对象引用将引用相同的对象。
So the changes you make to list[i].submitter
is affecting the original object as well.
因此,您对list [i] .submitter所做的更改也会影响原始对象。
If you don't want the update to happen, then you need to deep copy the objects inside array as well like below:
如果您不希望更新发生,那么您需要深层复制数组中的对象,如下所示:
function deepCopy (arr) {
var out = [];
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
var obj = {};
for (var k in item) {
obj[k] = item[k];
}
out.push(obj);
}
return out;
}
// test case
// 测试用例
var questionsModelOriginal = [
{'a' : 1, submitter : "Rias"},
{'b' : 2, submitter : "SO"}
];
var questionsModelCopy = deepCopy(questionsModelOriginal);
Now if you change the property of questions inside questionsModelCopy, it will not modify the global Questions model.
现在,如果您更改questionsModelCopy中的问题属性,它将不会修改全局问题模型。
#1
2
Array is always pass by reference in javascript. That's the reason why when you edit the array it affects the original array.
数组始终在javascript中通过引用传递。这就是为什么在编辑数组时它会影响原始数组的原因。
In your case, though you're making a copy of questionModel, still what your getting is a list of objects in your array. The objects reference in both the arrays will refer to same object.
在您的情况下,虽然您正在制作questionModel的副本,但您获得的仍然是数组中的对象列表。两个数组中的对象引用将引用相同的对象。
So the changes you make to list[i].submitter
is affecting the original object as well.
因此,您对list [i] .submitter所做的更改也会影响原始对象。
If you don't want the update to happen, then you need to deep copy the objects inside array as well like below:
如果您不希望更新发生,那么您需要深层复制数组中的对象,如下所示:
function deepCopy (arr) {
var out = [];
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
var obj = {};
for (var k in item) {
obj[k] = item[k];
}
out.push(obj);
}
return out;
}
// test case
// 测试用例
var questionsModelOriginal = [
{'a' : 1, submitter : "Rias"},
{'b' : 2, submitter : "SO"}
];
var questionsModelCopy = deepCopy(questionsModelOriginal);
Now if you change the property of questions inside questionsModelCopy, it will not modify the global Questions model.
现在,如果您更改questionsModelCopy中的问题属性,它将不会修改全局问题模型。