如何将数组值迭代到另一个数组的所有元素

时间:2016-05-20 06:42:13

标签: javascript angularjs foreach

我有2个阵列:汽车&自行车。 我想将汽车数组的每个元素与自行车数组的所有其他元素进行比较,如果两者相等,则提示true,否则为false。

$scope.getComparison = function() {
$scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
angular.forEach($scope.bikes, function(value, key) {
    $scope.p_Id = value.bikes;
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.cars, function(value, key) {
        $scope.b_Id = value.cars;
        if ($scope.p_Id == $scope.b_Id) {
            alert(b_Id + "=" + p_Id + "=" + "true");
        } else {
            alert(b_Id + "=" + p_Id + "=" + "false");
        }
    });
  });
};

我哪里错了?我需要帮助。

7 个答案:

答案 0 :(得分:2)

不工作!

var _firstArray = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
var _secondArray=["1", "2", "3", "4", "54", "55", "56", "56", "58"];

var areEqual = true;

_firstArray.forEach(function(item) {
    if (!(item in _secondArray)) {
        areEqual = false;
    }
});
console.log(areEqual);

编辑:我对“in”运算符错了。它仅检查给定对象的属性。

"45" in _firstArray将产生false,但0 in _firstArray将产生true,因为0是该数组的属性,'length' in _firstArray将结果也是true。这是一个有效的解决方案:

var _firstArray = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
var _secondArray=["1", "2", "3", "4", "54", "55", "56", "56", "58"];

_firstArray.forEach(function(item1) {
    _secondArray.forEach(function (item2) {
        if (item1 === item2) {
            console.log(item1 + '=' + item2 + '=' + 'true');
        } else {
            console.log(item1 + '=' + item2 + '=' + 'false');
        }
    });
});

答案 1 :(得分:1)

试试这个:

   $scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
   $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
   angular.forEach($scope.bikes, function(bike) {
      angular.forEach($scope.cars, function(car) {
        if (bike == car) {
          alert(bike + "=" + car + "=" + "true");
         } else {
             alert(bike  + "=" + car+ "=" + "false");
           }
     });
   });

答案 2 :(得分:1)

您可以使用forEach&用于查找匹配的indexOf数组方法

var _firstArray = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
var _secondArray=["1", "2", "3", "4", "54", "55", "56", "56", "58"];
_firstArray.forEach(function(item){
  if (_secondArray.indexOf(item) !==-1){
     console.log(item)  
  }
  else{
  console.log("not matched");
  }
})

注意:您的代码中可以使用相同的数组方法

选中此jsfiddle

答案 3 :(得分:0)

希望这就是你要找的东西

jsFiddle链接 - https://jsfiddle.net/U3pVM/25016/。用控制台替换警报

$scope.getComparison = function() {
$scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
angular.forEach($scope.bikes, function(value, key) {
    $scope.p_Id = value;
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.cars, function(value, key) {
        $scope.b_Id = value;
        if ($scope.p_Id == $scope.b_Id) {
            alert($scope.b_Id + "=" + $scope.p_Id + "=" + "true");
        } else {
            alert($scope.b_Id + "=" + $scope.p_Id + "=" + "false");
        }
    });
});
};

答案 4 :(得分:0)

forEach()方法的值参数已经代表了数组中的自行车,同样适用于汽车: 您可以像这样更改代码:

$scope.getComparison = function() {
$scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
angular.forEach($scope.bikes, function(value, key) {
    $scope.p_Id = value;
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.cars, function(value, key) {
        $scope.b_Id = value;
        if ($scope.p_Id == $scope.b_Id) {
            alert(b_Id + "=" + p_Id + "=" + "true");
        } else {
            alert(b_Id + "=" + p_Id + "=" + "false");
        }
    });
});
};

它应该有用。

您还可以嵌套forEach()方法,例如:

 $scope.getComparison = function() {
    $scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
    $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
    angular.forEach($scope.bikes, function(bikeValue, bikeKey) {
        angular.forEach($scope.cars, function(carValue, carKey) { 
            if (bikeValue == carValue) {
                alert(bikeValue + "=" + carValue + "=" + "true");
            } else {
                alert(bikeValue + "=" + carValue + "=" + "false");
            }
        });
    });
    };

这种方式直接比较它们而不将值保存到$scope

答案 5 :(得分:0)

将1个数组的每个元素与另一个数组的每个元素进行比较?

2 每个意味着使用2个循环。

答案 6 :(得分:0)

您的代码存在一些问题,请尝试使用以下代码并完美运行:

$scope.getComparison = function() {
                        $scope.bikes = ["1", "3", "3", "2", "6", "55", "45", "36", "18"];
                        angular.forEach($scope.bikes, function(value, key) {
                            $scope.p_Id = value;
                            $scope.cars = ["1", "2", "3", "4", "54", "55", "56", "56", "58"];
                            angular.forEach($scope.cars, function(value, key) {
                                $scope.b_Id = value;
                                if ($scope.p_Id == $scope.b_Id) {
                                    alert($scope.b_Id + "=" + $scope.p_Id + "=" + "true");
                                } else {
                                    alert($scope.b_Id + "=" + $scope.p_Id + "=" + "false");
                                }
                            });
                        });
                    };