这是我的代码我想检查一下,如果数组包含这个特定的字符串“Identicon”。我正在寻找一个代码作为解决方案,我只想查看条件。
$scope.profileImageOptions = [
{
Type: "Identicon",
Code: "identicon"
},
{
Type: "MonsterID",
Code: "monsterid"
},
];
if($scope.profileImageOptions.indexOf($rootScope.settings.defaultImage) >-1)
{
console.log('ok');
}
答案 0 :(得分:2)
您可以将includes
方法与some
方法结合使用。
some
方法接受callback
提供的函数作为参数,该函数适用于array
中的每个项目。
profileImageOptions = [
{
Type: "Identicon",
Code: "identicon"
},
{
Type: "MonsterID",
Code: "monsterid"
},
];
var exist=profileImageOptions.some(function(item){
return item.Type.includes("Identicon");
});
console.log(exist);
此外,您可以使用arrow function
来简化代码。
profileImageOptions.some(item => item.Type.includes("Identicon"))
答案 1 :(得分:0)
$scope.profileImageOptions.some(element => element.Type.includes('Identicon'));
让我们在Javascript中使用https://blog.magnusmontin.net/2013/04/20/implement-a-confirmation-dialog-in-wpf-with-mvvm-and-prism/:
$(function() {
$('input[name="daterange"]').daterangepicker();
$('input[name="daterange"]').change(function(){
$(this).val();
console.log($(this).val());
});
});
答案 2 :(得分:0)
你可以这样做:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<p>The name is {{ result[0].Type == "Identicon" ? "OK" : "Not OK"}}</p>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.profileImageOptions = [
{
Type: "Identicon",
Code: "identicon"
},
{
Type: "MonsterID",
Code: "monsterid"
},
];
$scope.result = $scope.profileImageOptions.filter(function(res){
return res.Type == "Identicon";});
});
</script>
</body>
</html>
检查example