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.
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'))