返回AngularJS中两个数组值之间的匹配

Return match between two array values in AngularJS

本文关键字:数组 之间 两个 AngularJS 返回      更新时间:2023-09-26

我在返回两个数组元素之间的匹配数组时遇到问题,请提供重要帮助这是我的代码

      $scope.MacA = [
  {
    name: '24:fd:52:c3:d8:35',
    sector: 'A23'
  }, 
  {
    name: '56:db:30:4b:57:45',
    sector: 'It support'
  },
  {
    name: 'b6:b6:76:6b:e9:00',
    sector: 'A24'
  },
  {
    name: 'e8:74:e6:a1:14:16',
    sector: 'Vodafone Guest'
  },
  {
    name: 'dc:4a:3e:b7:32:0e',
    sector: 'Direct HP officejet'
  }
  ,
  {
    name: '7c:4c:a5:32:13:29',
    sector: 'skyb7'
  }

]阵列2是

scope.match = ['dc:4a:3e:b7:32:0e','7c:4c:a5:32:13:29' ];

这是一个函数,如果找到,它会返回匹配

 $scope.getList = function(){
      // $scope.wifiList = WifiService.list();
      var c = $scope.MacA;
      var m = WifiService.list();
      for(var i = 0;i < c.length;i++) {
        for(var j = i;j < m.length;j++) { // Notice the j = i;
          if (c[i].name === m[j]) {
            $scope.result = c[i].sector;
        // $scope.result = 'Its working';
        break;
          } else {
             $scope.result = "Sorry!";
          }
        };
      };
  return $scope.result;
}

您没有指定您想要的结果,但这里有一个可能的版本来查找匹配项。

var result = [];
match.forEach(m => result.push(MacA.find(macA => macA.name === m)));
   MacA = [
  {
    name: '24:fd:52:c3:d8:35',
    sector: 'A23'
  }, 
  {
    name: '56:db:30:4b:57:45',
    sector: 'It support'
  },
  {
    name: 'b6:b6:76:6b:e9:00',
    sector: 'A24'
  },
  {
    name: 'e8:74:e6:a1:14:16',
    sector: 'Vodafone Guest'
  },
  {
    name: 'dc:4a:3e:b7:32:0e',
    sector: 'Direct HP officejet'
  },
  {
    name: '7c:4c:a5:32:13:29',
    sector: 'skyb7'
  }
]
Match = ['dc:4a:3e:b7:32:0e','7c:4c:a5:32:13:29' ];
MacA.filter(({name}) => Match.includes(name)).map(({sector}) => sector)
// RETURNS // ["Direct HP officejet", "skyb7"]

所以,考虑到你上面的代码,类似于这样的东西:

 $scope.getList = function(){
   return $scope.result = $scope.MacA.filter(({name}) => $scope.match.includes(name)).map(({sector}) => sector)
}

我重构了您的一些代码。

 $scope.getList = function(){
  var devices = $scope.MacA;
  var macList = WifiService.list();
  var results = devices.reduce((acc, device) => acc.concat(macList.find(current.name)? [current.sector]:[]), []);
  return results.length? $scope.results = results : $scope.results = 'Sorry!';
}