console.log 数组对象内的字符串与 angularjs

问题描述 投票:0回答:1

我想 console.log 或使用 angularjs 定位数组内对象的字符串,但它不起作用。请帮助我,我还是 angularjs 的新手。

这就是我想要做的。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profile Details AngularJS 1.X</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.6/angular.js"></script>
</head>
<body>
           <div class="container" ng-app="DetailApp">
           <div ng-controller="PostController">   
</div>
</div>

<script>

  var app = angular.module('DetailApp', []);
  app.controller('PostController', function($scope) {
                  
    $scope.details = [{firstname: "Dave", lastname: "Del Rio"}, {firstname: "Gerald", lastname: "Kingston"}, {firstname: "Larry", lastname: "Jackson"}];
    
    if ($scope.details.firstname == "Dave Del Rio") {
      console.log("Hello Dave!")
    }  
    else {
      console.log("sorry, You're not welcome!")
    };  
                  
  });
  
  </script>

</body>
</html>

我想在数组对象中定位名字“Dave”,但它根本不起作用。

javascript arrays angularjs object angularjs-scope
1个回答
0
投票

您可以使用 Array#some 来检查数组中的任何元素是否匹配条件。

if ($scope.details.some(x => x.firstname === 'Dave' && x.lastname === 'Del Rio'))
© www.soinside.com 2019 - 2024. All rights reserved.