如何比较两个函数不同的数组?

问题描述:

var app2 = angular.module("webApp2", [])
    .controller("webCtrl2", function ($scope, $window, $state, $http) {
        console.log("hi " + $state.params.usermail);
        $scope.uName = $state.params.usermail;
        $scope.useriden = $state.params.user;

        console.log("hbdhakkdjf" + "    " + $scope.uName)
        //$scope.data = $window.sessionStorage.getItem("Mydata");
        //console.log("data "+$scope.data);
        var usersList = [];
        var frndsAdded = [];

        $http.get("http://192.168.2.3:3000/userslist")
            .then(function (response) {
                usersList = response.data;
                //$scope.friendsList = response.data;
                //.................................................................
                $http.get("http://192.168.2.3:3000/sendfriendrequests/" + $scope.uName)
                    .then(function (response) {
                            frndsAdded = response.data;
                        },
                        function (response) {
                            console.log("error");
                        });
                // console.log("array print" + JSON.stringify($scope.usersList));
            }, function (response) {
                console.log("response");
            });
        console.log(frndsAdded);
        console.log("print userid" + $scope.useriden);
        $scope.addFrnd = function (frnd, index) {
            var data = {
                "userId": $scope.useriden,
                "requestname": frnd,
                "username": $scope.uName
            };
            console.log(data);
            var req = {
                method: 'POST',
                url: 'http://192.168.2.3:3000/friendrequests',
                data: data
            }
            $http(req).then(function (response) {
                console.log("hjhuhjh" + JSON.stringify(response.data));
                $scope.usersList.splice("index");
            }, function (response) {
                console.log(JSON.stringify(response));
            });
        }
        $scope.logoutFn = function () {
            $state.go("signup");
        }

    });

<nav class="navbar navbar-default well well-sm">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">WebChat</a>
        </div>
         <ul class="nav navbar-nav pull-right">
            <li><a href="#">Home</a></li>
            <li style = margin-top:5%>
                <div class = "dropdown">
                    <a href="#" data-toggle="dropdown">
                        <i class="glyphicon glyphicon-user"></i>Add Friends<span class = "caret"></span>
                    </a>
                <ul class = "dropdown-menu list-group" style = " overflow: scroll">
                    <!--<li class = "list-group-item" ng-repeat = "seeFrnd in usersList track by $index" ng-bind = "seeFrnd.username">name
                        <button class = "btn btn-primary" ng-click = "addFrnd()">Add Friend</button>
                    </li>-->
                    <li class = "list-group-item" ng-repeat = "seeFrnd in usersList track by $index"><span ng-bind="seeFrnd.username"></span>
                        <button class = "btn btn-primary btn-sm" ng-click = "addFrnd(seeFrnd.username,$index)" class = "buttn" style="float: right">Add Friend</button>
                    </li>
                </ul>
                </div>
            </li>
            
            <li>
               <a href="#" ng-bind="uName"></a>
           </li>
           <li><a href="#" ng-click="logoutFn()">Logout</a></li>

        </ul>
        
    </div>
</nav>
<div ui-view></div>

我在这里做聊天应用程序,我得到两个数组.一个数组用于用户列表,另一个数组用于朋友列表.两个数组的功能不同.我需要比较两个数组.我该怎么做?

I am doing chat application here I am getting two arrays.one is for users list and other is for friends list.both arrays are in different functions.I need to compare that both arrays.how could i do that?

在$ scope上定义两个数组.在获取第二个数组值之后,您必须使用逻辑比较第二个API调用中的两个数组.如果要在外部执行此操作,则必须等待异步API调用完成,这样您才能获得完整的数组.

Define the two arrays on $scope. You have to use your logic to compare two arrays inside the second API call after getting the second array values. If you want to do it outside then you have to wait for the asychronous API call to finish, so that you will get the complete array.

 $scope.usersList = [];
    $sccope.frndsAdded = [];
    $http.get("http://192.168.2.3:3000/userslist").then(function(response) {
        $scope.usersList = response.data;
        //$scope.friendsList = response.data;
        //.................................................................
        $http.get("http://192.168.2.3:3000/sendfriendrequests/" + $scope.uName).then(function(response) {
            $scope.frndsAdded = response.data;
            //Comaprison Logic
            for (var i = 0; i < $scope.usersList.length; i++) {
                for (var j = 0; j < $scope.frndsAdded.length; j++) {
                    if ($scope.usersList[i].request == $scope.frndsAdded[j].request)
                    //ur logic
                }
            }
        }, function(response) {
            console.log("error");
        });
        // console.log("array print" + JSON.stringify($scope.usersList));
    }, function(response) {
        console.log("response");
    });