console.log字符串在带有AngularJS的数组对象内。

huangapple go评论60阅读模式
英文:

console.log string inside array object with angularjs

问题

I want to target the firstname "Dave" inside the array object but It doesn't work at all.

我想要定位数组对象中的firstname "Dave",但根本不起作用。

英文:

I want to console.log or target the string of an object inside array with angularjs but it doesn't work. Please help me I'm still new on angularjs.

Here is what I'm trying to do.

<!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>

I want to target the firstname "Dave" inside the array object but It doesn't work at all.

答案1

得分: 0

你可以使用 Array#some 来检查数组中是否有任何元素满足条件。

if ($scope.details.some(x => x.firstname === 'Dave' && x.lastname === 'Del Rio'))
英文:

You can use Array#some to check if any element in an array matches a condition.

if ($scope.details.some(x => x.firstname === 'Dave' && x.lastname === 'Del Rio'))

huangapple
  • 本文由 发表于 2023年2月19日 12:03:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定