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

时间:2017-06-22 11:58:56

标签: javascript angularjs

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>

我在这里做聊天应用程序我得到两个数组。一个用于用户列表,另一个用于朋友列表。两个数组在不同的函数中。我需要比较两个数组。我可以这样做吗?

2 个答案:

答案 0 :(得分:0)

angular.forEach(arr1, function(value1, key1) {
        angular.forEach(arr2, function(value2, key2) {
            //Comparison logic
        });
    });

答案 1 :(得分:0)

在$ scope上定义两个数组。在获取第二个数组值之后,必须使用逻辑来比较第二个API调用中的两个数组。如果你想在外面做,那么你必须等待异步API调用完成,这样你才能获得完整的数组。

 $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");
    });