1

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.

7
  • Are you trying to check every element in the array or just the first element? Commented Feb 19, 2023 at 4:05
  • yes I just I want to check every element in the array Commented Feb 19, 2023 at 4:09
  • i jus want to check if the element inside the object and array is correct. Commented Feb 19, 2023 at 4:14
  • Perhaps you want if ($scope.details.some(x => x.firstname === 'Dave' && x.lastname === 'Del Rio')) Commented Feb 19, 2023 at 4:14
  • also want to target the string inside the array object Commented Feb 19, 2023 at 4:16

1 Answer 1

0

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'))
1
  • thanks well explained! sorry for my english I'm asian and I can't clearly explain my problem but you still give what I want :) Commented Feb 19, 2023 at 4:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.