Figure out what this code means.

my logic is a little poor. I"m still in the learning stage. I can"t understand this code. I"d like to ask you

                        if($scope.userList.findIndex(it => {
                            return it.accountID == item.accountId
                                    }) != -1) {
                                    item.checked = true
                                } else {
                                    item.checked = false
                              }
Jun.13,2022

look up the item in $scope.userList where accountId is item.accountId, and return true if any, but no false. This code is a bit indented


$scope


The

code is to find the index of the user in userList where accountId is the same as item.accountId.

findIndex returns a number indicating the index

The

index is equal to-1, which means that such user does not exist, and if the index is greater than-1, it exists.

if such a user exists, then item.checked is set to true and vice versa to false.


findIndex returns the position of a qualified element in the array, and there is no return of-1.

this code determines whether there is an object in userList that specifies accountId

.

findIndex reference documentation


if($scope.userList.findIndex(it => {return it.accountID == item.accountId}) != -1) {
    item.checked = true
} else {
    item.checked = false
}

first look at it = > {return it.accountID = = item.accountId} his result is Boolean true or false
looking at the result of $scope.userList.findIndex (true or false)

 -1    item.checked = true  
 -1   item.checked = false 

it should be written as follows:

item.checked = $scope.userList.findIndex(it => {return it.accountID == item.accountId}) != -1

if accountId and item exist in userList , set checked in item to true .

Menu