与数组索引值比较

时间:2015-12-25 06:07:31

标签: angularjs ionic-framework

imagevalue数组的长度为6。

 $scope.imageValue=[];

imageArray的长度为73。

$scope.imageArray=[];

我使用for和if循环检查imagevalue数组中的任何索引是否等于imageArray中的任何索引。

for(var i = 0;i <= $scope.names.length;i++) {
    for(var j=0;j<=$scope.imageID.length;j++){
        if($scope.imageValue[i] == $scope.imageArray[j]) {
            $scope.testArray.push($scope.imageID[j].bindaryData);
            console.log($scope.testArray);
            return true;
        }
        else if($scope.imageValue[i] !== $scope.imageArray[j]){
            return false;
        }

    }
}

如果是,那么我必须在空数组bindaydata中推送testArray

$scope.testArray=[];

我的所有代码都是

angular.module('ob3App.lead')
    .controller('LeadProductCtrl',['$scope','$http','filterFilter', function($scope,$http,filterFilter) {

    $scope.namegetfunction = function() {
      $scope.imageValue=[];
    $http.get("http://5.9.42.45:8080/easycloud1/org.openbravo.service.json.jsonrest/Product?l=saiy5k&p=saiy5k")
        .success(function(response) {
        console.log(response);
        $scope.names = response.response.data;
        console.log($scope.names.length);
        $scope.names.forEach(function(ids){
            console.log(ids.id);
            $scope.imageValue.push(ids.image);
        })
        $scope.imageGetFunction();
        console.log($scope.imageValue);
        })
        .error(function(response){
            alert('error in name');
        });
    };
    $scope.namegetfunction();

    $scope.imageGetFunction = function() {
        $scope.imageArray=[];
        $scope.testArray=[];

        $http.get("http://5.9.42.45:8080/easycloud1/org.openbravo.service.json.jsonrest/ADImage?l=saiy5k&p=saiy5k")
        .success(function(response) {
            console.log(response);
            $scope.imageID = response.response.data;
            console.log($scope.imageID.length);

            $scope.imageID.forEach(function(item) {
                $scope.imageArray.push(item.id);
            })

            console.log($scope.imageArray);

            for(var i = 0;i <= $scope.names.length;i++) {
                for(var j=0;j<=$scope.imageID.length;j++){
                    if($scope.imageValue[i] == $scope.imageArray[j]) {
                        $scope.testArray.push($scope.imageID[j].bindaryData);
                        console.log($scope.testArray);
                        return true;
                    }
                    else if($scope.imageValue[i] !== $scope.imageArray[j]){
                        return false;
                    }

                }
            }
              console.log($scope.testArray);

        })
        .error(function(response){
            console.log(response);
            alert('error in image');
        });
    };

 }]);

2 个答案:

答案 0 :(得分:0)

$scope.imageId[j].bindaryData 

应该是

$scope.imageID[j].bindaryData

http://plnkr.co/edit/kGCln96vqetu5CqnloYK?p=preview

答案 1 :(得分:0)

检查一下它工作正常

for(var indexOfProduct = 0;indexOfProduct< $scope.productImage.length;indexOfProduct++){
            console.log(indexOfProduct);
            console.log($scope.productImage.length);
            if($scope.productImage[indexOfProduct].image == null) {
                    console.log($scope.productImage[indexOfProduct].image);
                    $scope.base64Array.push(null);
                    console.log($scope.base64Array);

                }
                else if($scope.productImage[indexOfProduct].image !== null) {
                    for(var indexOfAdImage =0;indexOfAdImage<=$scope.imageID.length;indexOfAdImage++){
                    console.log(indexOfAdImage);
                        if($scope.productImage[indexOfProduct].image == $scope.imageID[indexOfAdImage].id) {
                            console.log($scope.imageID[indexOfAdImage].bindaryData);
                            $scope.base64Array.push($scope.imageID[indexOfAdImage].bindaryData);
                            console.log($scope.base64Array);
                            break;
                        }
                    }    
                }
            }