Shalindri
Shalindri

Reputation: 3

How to check if a json array has a particular value for a key? Angular JS

 test_array = [  {"id":1,"title":"test1","type":"A"  },  
                 {"id":2,"title":"test2","type":"B"  },  
                 {"id":3,"title":"test3","type":"A"  },
              ]


var x= test_array.isContain("type"=="B") 

I want a method like I stated above and get an return value

Upvotes: 0

Views: 235

Answers (1)

Narkhede Tushar
Narkhede Tushar

Reputation: 683

You can do something like this:

test_array = [  {"id":1,"title":"test1","type":"A"  },  
                 {"id":2,"title":"test2","type":"B"  },  
                 {"id":3,"title":"test3","type":"A"  },
              ]

Array.prototype.isExist = function(key, value){ 
      return this.some(function(obj){ 
          return obj[key] === value;
      });
};

test_array.isExist('type', 'B');

Upvotes: 1

Related Questions