Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

console.log string inside array object with angularjs

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading