TechGuy TechGuy - 1 year ago 86
jQuery Question

Array contains doesn't capture

I have a array and array looks like this,I want to know about the is NewsType ="Int" included in the array.

array is NewsTypeArray.

When i debug in firebug.array looks like this,

0 Object { NewsType="Local"}
1 Object { NewsType="Int"}

var InternationalNewsfound = $.inArray("Int", NewsTypeArray) > -1;

InternationalNewsfound is shows me false.

Answer Source

Use a filter with jquery .grep(),

var NewsTypeArray = [{"NewsType": "Local"}, {"NewsType": "Int"}];

var InternationalNewsfound = $.grep(NewsTypeArray, function(obj) { 
                                 return obj.NewsType === "Int"; 
                             }).length > 0;
<script src=""></script>

*Note : While just .length would help you check for truthiness you could still use .length > 0 for the flag.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download