2

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.

1
  • never use 'async': false ! It is a terrible practice and browser vendors have deprecated it. You should be seeing warnings in your console Commented Sep 20, 2016 at 22:50

1 Answer 1

1

If val.countries is an array, you should be able to use something like this instead:

return val.program_type === 'this is A type' && val.countries.indexOf(varCountry) > 0;
Sign up to request clarification or add additional context in comments.

5 Comments

This is what I was looking for thanks. Now it won't work in IE11 however.
var varCountry = $( "body" ).data( "country"); var programsJSON = null; var a_Programs; $.ajax({ 'async': false, 'global': false, 'url': '/ProgramListing', 'dataType': "json", 'success': function (data) { programsJSON = data; countList(); } });
function countList(){ 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; };
I wanted to show the working code that isn't working in IE: Obviously I don't know how to ask questions. I know If I ask another questioin sombody will flag as a duplicate.
Sorry, I forgot that IE11 doesn't support includes. Please see my modified answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.