0

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

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?

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?

Notes: I'm using AngularJS with ASP.NET Web API 2 to get the data from database.

3
  • If you want to remove $hashkey and like this other do angular.toJson() it will remove man Commented Jun 15, 2016 at 4:17
  • @SakthiSureshAnand, great, thanks for the information, so in angular to print the json, should it be used angular.toJson() rather than JSON.stringify()? Commented Jun 15, 2016 at 4:21
  • I suggest you to go underscore.js underscorejs.org/[][1] it has all the methods to iterate like stuff Commented Jun 15, 2016 at 4:24

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, it works, but rather than iterate usersSelected, can we use indexOf to get the object? before using $.grep I used array.indexOf() but it didn't work.
because you are iterating over array of json which is basically objects so you will get the index of the array but you need to iterate over the userid present as a ke value type inside the json so for that you have to use the basic approach to iterate through the json
1

tested with 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]);
  }


}

4 Comments

since you're using angular, the $ is not jquery. can you try converting that to for loop
yeah but it still doesn't work, here is the code var users2 = $scope.users; var usersSelected2 = $scope.usersSelected; console.log('users2 = ' + JSON.stringify(users2)); $(users2).each(function (i, d) { if (usersSelected2.some(function (e) { e.UserID == d.UserID })) { d.Selected = true; } }); console.log('users2 = ' + JSON.stringify(users2));
edited the answers, can you run that exact code and see.
I've debugged it, and the condition never be true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.