1

I have user_id in two array, one is available user_id's for projects and other is selected user_id.

availableusers = ["00000000a447a9160c9d6b74", "000000005c05c11764445bdb"]
selected Users = ["000000005c05c11764445bdb"]

Now I need to match both the array and have to display matched in selected div and unmatched in availabe div.

How can I able to do this?

FabioBranch
  • 175
  • 4
  • 19
Vishnu
  • 745
  • 12
  • 32
  • What is angularjs in this? – haMzox Sep 21 '17 at 09:35
  • Possible duplicate of [What is the fastest or most elegant way to compute a set difference using Javascript arrays?](https://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javasc) – 31piy Sep 21 '17 at 09:55

2 Answers2

2

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

  $scope.availableusers = ["00000000a447a9160c9d6b74", "000000005c05c11764445bdb"]
  $scope.selectedUsers = ["000000005c05c11764445bdb"]

  $scope.matchUser = [];
  for (var i = 0; i < $scope.availableusers.length; i++) {
    for (var j = 0; j < $scope.selectedUsers.length; j++) {
      if ($scope.availableusers[i] === $scope.selectedUsers[j]) {
        $scope.matchUser.push($scope.availableusers[i]);
      }
    }

  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <span>Available users</span>
  <div ng-repeat="available in availableusers">{{available}}</div>
  <span>Selected Users</span>
  <div ng-repeat="selectUser in selectedUsers">{{selectUser}}</div>
  <span>Users</span>
  <div ng-repeat="Users in matchUser">{{Users}}</div>
</div>
Lakmi
  • 1,379
  • 3
  • 16
  • 27
1
var result=[];
 for(var i = 0 ;i<availableUsers.length;i++){
   if(availableusers[i] === selectedUsers[i])
     {
       result.push(availableusers[i]);
      }     
  }
P J
  • 70
  • 1
  • 4
  • 25
  • 4
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Sep 21 '17 at 12:12