[英]Updating the value of an object inside a loop using javascript
I'm currently facing a difficulty in my codes. 我目前在编码方面遇到困难。
First i have an array of objects like this [{Id:1, Name:"AML", allowedToView:"1,2"}, {Id:2, Name:"Res", allowedToView:"1"}...]
which came from my service 首先,我有一个像这样的对象数组
[{Id:1, Name:"AML", allowedToView:"1,2"}, {Id:2, Name:"Res", allowedToView:"1"}...]
来自我的服务
I assign it in variable $scope.listofResource
我在变量
$scope.listofResource
分配它
Then inside of one of my objects I have that allowedToView
key which is a collection of Id's of users that I separate by comma. 然后,在我的一个对象中,我有那个
allowedToView
键,它是我用逗号分隔的用户ID的集合。
Then I have this code... 然后我有这段代码...
Javascript Java脚本
$scope.listofResource = msg.data
for (var i = 0; i < msg.data.length; i++) {
First I run a for loop so I can separate the Id's
of every user in allowedToView
key 首先,我运行一个for循环,以便可以在
allowedToView
键中分离每个用户的Id's
var allowed = msg.data[i].allowedToView.split(",");
var x = [];
Then I create a variable x
so I can push a new object to it with a keys of allowedId
that basically the Id of the users and resId
which is the Id of the resource 然后,我创建一个变量
x
以便可以使用一个allowedId
键(基本上是用户的ID)和resId
它是资源的ID)将一个新对象推入该变量
for (var a = 0; a < allowed.length; a++) {
x.push({ allowedId: allowed[a], resId: msg.data[i].Id });
}
Then I put it in Promise.all
because I have to get the Name of that "allowed users" base on their Id's using a service 然后,将其放在
Promise.all
因为我必须基于他们的ID使用服务来获取“允许的用户”的名称
Promise.all(x.map(function (prop) {
var d = {
allowedId: parseInt(prop.allowedId)
}
return ResourceService.getAllowedUsers(d).then(function (msg1) {
msg1.data[0].resId = prop.resId;
Here it returns the Id and Name of the allowed user. 在这里,它返回允许用户的ID和名称。 I have to insert the
resId
so it can pass to the return object and it will be displayed in .then()
below 我必须插入
resId
以便它可以传递给返回对象,并将显示在下面的.then()
中
return msg1.data[0]
});
})).then(function (result) {
I got the result that I want but here is now my problem 我得到了想要的结果,但是现在这是我的问题
angular.forEach(result, function (val) {
angular.forEach($scope.listofResource, function (vv) {
vv.allowedToView1 = [];
if (val.resId === vv.Id) {
vv.allowedToView1.push(val);
I want to update $scope.listofResource.allowedToView1
which should hold an array of objects and it is basically the info of the allowed users. 我想更新
$scope.listofResource.allowedToView1
,它应该包含一个对象数组,它基本上是允许用户的信息。 But whenever I push a value here vv.allowedToView1.push(val);
但是每当我在此处推送值时,
vv.allowedToView1.push(val);
It always updates the last object of the array. 它总是更新数组的最后一个对象。
}
})
})
});
}
So the result of my code is always like this [{Id:1, Name:"AML", allowedToView:"1,2", allowedToView:[]}, {Id:2, Name:"Res", allowedToView:"1", allowedToView:[{Id:1, Name:" John Doe"}]}...]
因此,我的代码的结果始终像这样
[{Id:1, Name:"AML", allowedToView:"1,2", allowedToView:[]}, {Id:2, Name:"Res", allowedToView:"1", allowedToView:[{Id:1, Name:" John Doe"}]}...]
The first result is always blank. 第一个结果始终为空白。 Can anyone help me?
谁能帮我?
Link to the solution - Plunkr 链接到解决方案-Plunkr
for (var i = 0; i < msg.length; i++) {
var allowed = msg[i].allowedToView.split(",");
msg[i].allowedToView1 = [];
var x = [];
Like Aleksey Solovey correctly pointed out, the initialization of the allowedToView1 array is happening at the wrong place. 就像Aleksey Solovey正确指出的那样,allowedToView1数组的初始化发生在错误的位置。 It should be shifted to a place where it is called once for the msg.
应该将其转移到味精一次被调用的地方。 I've shifted it to after allowedToView.split in the first loop as that seemed a appropriate location to initialize it.
我在第一个循环中将其移至allowableToView.split之后,因为这似乎是初始化它的适当位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.