【问题标题】:How to iterate an array value to all the elements of another array如何将数组值迭代到另一个数组的所有元素
【发布时间】:2016-05-20 06:42:13
【问题描述】:

我有 2 个数组:汽车和自行车。 我想将汽车数组的每个元素与自行车数组的所有其他元素进行比较,如果两者相等,则警报为真,否则为假。

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

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

【问题讨论】:

  • "value.bikes" ?检查控制台是否有错误..

标签: javascript angularjs foreach


【解决方案1】:

不工作!

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');
        }
    });
});

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

      您可以使用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

      【讨论】:

        【解决方案4】:

        希望这就是你要找的东西

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

        【讨论】:

          【解决方案5】:
          forEach() 方法的

          value 参数已经代表了数组中的自行车,汽车也是如此: 您可以像这样更改代码:

          $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

          【讨论】:

            【解决方案6】:

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

            2 每个的意思是使用2个循环就是这样。

            【讨论】:

              【解决方案7】:

              你的代码有问题,试试下面的代码应该可以正常工作:

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

              【讨论】:

              • 您应该解释一下问题所在。
              • 问题在于 value.bikes 和 value.cars,实际上我们必须引用 value 而不是 value.bikes,因为它什么都不是。所以我们在上面我添加的代码中进行了更正。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-04-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多