 <p>Instead of using 2 nested loops, convert <code>$scope.userList</code> into an object that has the <code>userID</code> as the key. Then you can loop through your <code>userID</code> array and quickly check if a user with the same key exists in your new object.</p> <p>By removing the nested loops, the code below runs in linear time instead of n^2, which is beneficial if you have large arrays. And if you store <code>$scope.userList</code> as an object that's keyed by its <code>userId</code>, then you can save even more time by not having to create the index each time the function is run.</p> <pre><code>$scope.userInfo = function(userID) { var userList = {}; //create object keyed by user_id for(var i=0;i&lt;$scope.userList.length;i++) { userList[$scope.userList._id] = $scope.userList; } //now for each item in userID, see if an element exists //with the same key in userList created above var userDetails = []; for(i=0;i&lt;userID.length;i++) { if(userID[i] in userList) { userDetails.push(userList[userID[i]]); } } return userDetails; }; </code></pre>