I need to produce a number that is a total number of JSON objects, for instance: "6 type_A programs in your country". I have a JSON and have gotten it to filter a the type and country.
each object in the JSON contains a "countries" array (some with many countries, some with only one):
"countries":["China","Singapore","U.S.A.","United Kingdom"]
The JS:
var varCountry = $( "body" ).data( "country");
var programsJSON = (function () {
var programsJSON = null;
var a_Programs;
$.ajax({
'async': false,
'global': false,
'url': '/ProgramListing',
'dataType': "json",
'success': function (data) {
programsJSON = data;
}
});
var find_A = programsJSON.Programs.filter(function(val, index, array) {
return val.program_type === 'this is A type' && val.countries[0] === varCountry;
});
a_Programs = find_A.length;
$(".program-count.a_Programs").text(a_Programs);
return programsJSON;
})();
Currently it almost seems to work, it looks through the JSON, it filters all categories that match a_Programs && match a country in the list based on a passed var. Then below it counts the total length and creates a number as a var. This script works but is not accurate -- I think it only counts when the country is === to the countriy listed first in it's array only and ignores it if it is 2nd, 3rd, etc, not sure though.
The problem seems to be here:
val.countries[0] === varCountry
How it should work: If the country is "China" for instance, I want this to look through the JASON and find the program type && the country array and return the total number of JSON objects that are both a_Program && in the local country.
'async': false! It is a terrible practice and browser vendors have deprecated it. You should be seeing warnings in your console