英文:
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'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论