I have an array of object
我有一个对象数组
// This is should be taken from database
$scope.users = [{"$id":"1","UserID":3,"Name":"A","Selected":false},{"$id":"2","UserID":4,"Name":"B","Selected":false},{"$id":"3","UserID":5,"Name":"C","Selected":false},{"$id":"4","UserID":6,"Name":"D","Selected":false}]
Then there is an array that contains the selected users from previous screen
然后是一个包含上一屏幕中所选用户的数组
$scope.usersSelected = [{"$id":"3","UserID":5,"Name":"C","Selected":true,"$$hashKey":"object:83"},{"$id":"4","UserID":6,"Name":"D","Selected":true,"$$hashKey":"object:84"}]
I want to update the Selected
properties of $scope.users
if they are found in $scope.usersSelected
. So I iterate the $scope.usersSelected
then search its UserID
in the UserID
of $scope.users
我想更新$ scope.users的Selected属性,如果它们在$ scope.usersSelected中找到。所以我迭代$ scope.usersSelected,然后在$ scope.users的UserID中搜索其UserID
for (var i = 0; i < $scope.usersSelected.length; i++) {
var obj = $.grep($scope.users, function (e) { return e.UserID == $scope.usersSelected[i].UserID; });
obj.Selected = true;
}
but the Selected
properties won't be updated. What's wrong with the code above?
但是不会更新Selected属性。上面的代码有什么问题?
Second, I need an explanation regarding the data in array above, why there is always "$id"
when I populate the data from the database and also "$$hashKey"
when I get the data from another screen? Where do these values come from?
其次,我需要一个关于上面数组中数据的解释,为什么当我从数据库填充数据时总是有“$ id”,当我从另一个屏幕获取数据时,还有“$$ hashKey”?这些价值来自哪里?
Notes: I'm using AngularJS with ASP.NET Web API 2 to get the data from database.
注意:我正在使用AngularJS和ASP.NET Web API 2从数据库中获取数据。
2 个解决方案
#1
3
tested working absolutely fine
测试工作绝对正常
$scope.users = [ {
"$id" : "1",
"UserID" : 3,
"Name" : "A",
"Selected" : false
}, {
"$id" : "2",
"UserID" : 4,
"Name" : "B",
"Selected" : false
}, {
"$id" : "3",
"UserID" : 5,
"Name" : "C",
"Selected" : false
}, {
"$id" : "4",
"UserID" : 6,
"Name" : "D",
"Selected" : false
} ];
$scope.usersSelected = [ {
"$id" : "3",
"UserID" : 5,
"Name" : "C",
"Selected" : true,
"$$hashKey" : "object:83"
}, {
"$id" : "4",
"UserID" : 6,
"Name" : "D",
"Selected" : true,
"$$hashKey" : "object:84"
} ]
var users = $scope.users;
var usersSelected = $scope.usersSelected;
for (var i = 0; i < users.length; i++) {
for (var j = 0; j < usersSelected.length; j++) {
debugger
if (users[i].UserID == usersSelected[j].UserID) {
console.log(users[i].UserID)
console.log(usersSelected[j].UserID)
users[i].Selected = true;
}
}
}
console.log(users)
console.log(usersSelected);
#2
1
tested with javascript.
用javascript测试。
usersSelected = $scope.usersSelected;
users = $scope.users;
for (var i=0;i<users.length;i++){
if (usersSelected.some(function(e) e.UserID == users[i].UserID)) {
users[i].Selected = true;
console.dir(users[i]);
}
}
#1
3
tested working absolutely fine
测试工作绝对正常
$scope.users = [ {
"$id" : "1",
"UserID" : 3,
"Name" : "A",
"Selected" : false
}, {
"$id" : "2",
"UserID" : 4,
"Name" : "B",
"Selected" : false
}, {
"$id" : "3",
"UserID" : 5,
"Name" : "C",
"Selected" : false
}, {
"$id" : "4",
"UserID" : 6,
"Name" : "D",
"Selected" : false
} ];
$scope.usersSelected = [ {
"$id" : "3",
"UserID" : 5,
"Name" : "C",
"Selected" : true,
"$$hashKey" : "object:83"
}, {
"$id" : "4",
"UserID" : 6,
"Name" : "D",
"Selected" : true,
"$$hashKey" : "object:84"
} ]
var users = $scope.users;
var usersSelected = $scope.usersSelected;
for (var i = 0; i < users.length; i++) {
for (var j = 0; j < usersSelected.length; j++) {
debugger
if (users[i].UserID == usersSelected[j].UserID) {
console.log(users[i].UserID)
console.log(usersSelected[j].UserID)
users[i].Selected = true;
}
}
}
console.log(users)
console.log(usersSelected);
#2
1
tested with javascript.
用javascript测试。
usersSelected = $scope.usersSelected;
users = $scope.users;
for (var i=0;i<users.length;i++){
if (usersSelected.some(function(e) e.UserID == users[i].UserID)) {
users[i].Selected = true;
console.dir(users[i]);
}
}