6

this is my code i want to check that if array contains this specific string "Identicon". and i'm looking for one line code for as solution i just want to check with if condition.

 $scope.profileImageOptions = [
                      {
                Type: "Identicon",
                Code: "identicon"
            },
            {
                Type: "MonsterID",
                Code: "monsterid"
            },

        ];

    if($scope.profileImageOptions.indexOf($rootScope.settings.defaultImage) >-1)
{
    console.log('ok');

    }
4
  • Possible duplicate of Get JavaScript object from array of objects by value or property Commented Apr 25, 2017 at 10:11
  • @Lakmi, it is working my solution ? Commented Apr 25, 2017 at 11:51
  • @Nguyen Thanh NO because, i'm looking for one line code. I got solution from this post. thnx Commented Apr 25, 2017 at 12:29
  • @Alexandru, YES your solution working for me thnx Commented Apr 25, 2017 at 12:30

3 Answers 3

6

You can use includes method in combination with some method.

some method accepts as parameter a callback provided function which is applied for every item in the array.

profileImageOptions = [
            {
                Type: "Identicon",
                Code: "identicon"
            },
            {
                Type: "MonsterID",
                Code: "monsterid"
            },

];
var exist=profileImageOptions.some(function(item){
  return item.Type.includes("Identicon");
});
console.log(exist);

Also, you can use an arrow function to simplify your code.

profileImageOptions.some(item => item.Type.includes("Identicon"))
Sign up to request clarification or add additional context in comments.

Comments

0

var arr = [
                      {
                Type: "Identicon",
                Code: "identicon"
            },
            {
                Type: "MonsterID",
                Code: "monsterid"
            },

        ];
var result = arr.some(element => element.Type.includes('Identicon'));
console.log(result)

Let's use some in Javascript:

$scope.profileImageOptions.some(element => element.Type.includes('Identicon'));

2 Comments

Thnak you for help.
Glad to know :)
0

You can do like below:

<!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>

Check the example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.