Stefan Hagen
Stefan Hagen

Reputation: 658

Reading out JSON values with Jquery

I'm trying to create 2 arrays from a JSON feed, and then calculate the average value of both. First, the way I will the arrays is like this:

$.getJSON('jasonfile.json', function(rawdata) {
  var array_one = [];
  var array_two = [];

  $.each(rawdata, function(i, entry) {
    if(entry["geslacht"] == "Man") {
      array_one.push(entry["key"]);
    else {
      array_two.push(entry["key"]);
    }
  )};
)};

This works fine, except when I look into my console array_one seems to be somekind of array of objects, and `array_two seems to be a normal array. Thus when I try to calculate the average of the two like so:

var array_one_total = 0;
$.each(array_one, function() {
  array_one_total += this;
});

array_one_average = array_one_total/array_one.length;

I get a right and a wrong value. array_one logs "NaN" like this in my console, while array_two displays the correct average:

Javascript weird result in console

It seems to be a problem with the format of the array, but I can't understand why is returns the second value correct, as I'm using the same method for both of them.

Here's in image of what happens if I log both arrays directly to the console, and you can see the different ways they are displayed:

console log

Well I got a lot of responses asking for my entire code, the number of arrays make it harder to read but here it is:

$.getJSON('propedeuse201112.json', function(rawdata) {
  var ucd_male = [];
  var ucd_female = [];
  var internetstandaarden_male = [];
  var internetstandaarden_female = [];
  var understanding_design_male = [];
  var understanding_design_female = [];
  var taal_in_context_male = [];
  var taal_in_context_female = [];
  var vormgeving_male = [];
  var vormgeving_female = [];
  var marketing_male = [];
  var marketing_female = [];
  var h_en_c_male = [];
  var h_en_c_female = [];
  var programmeren_male = [];
  var programmeren_female = [];
  var m_en_i_male = [];
  var m_en_i_female = [];
  var mediageschiedenis_male = [];
  var mediageschiedenis_female = [];
  var business_mapping_male = [];
  var business_mapping_female = [];
  var plug_and_play_male = [];
  var plug_and_play_female = [];
  var slc_male = [];
  var slc_female = [];

  $.each(rawdata, function(i, entry) {
    if(entry["geslacht"] == "Man") {
      ucd_male.push(entry["ucd"]);
      internetstandaarden_male.push(entry["internetstandaarden"]);
      understanding_design_male.push(entry["understanding design"]);
      taal_in_context_male.push(entry["Taal in context"]);
      vormgeving_male.push(entry["Vormgeving"]);
      marketing_male.push(entry["Marketing"]);
      h_en_c_male.push(entry["H&C"]);
      programmeren_male.push(entry["Programmeren"]);
      m_en_i_male.push(entry["M&I"]);
      mediageschiedenis_male.push(entry["Mediageschiedenis"]);
      business_mapping_male.push(entry["Business Mapping"]);
      plug_and_play_male.push(entry["Plug & Play"]);
      slc_male.push(entry["SLC"]);
    } else {
      ucd_female.push(entry["ucd"]);
      internetstandaarden_female.push(entry["internetstandaarden"]);
      understanding_design_female.push(entry["understanding design"]);
      taal_in_context_female.push(entry["Taal in context"]);
      vormgeving_female.push(entry["Vormgeving"]);
      marketing_female.push(entry["Marketing"]);
      h_en_c_female.push(entry["H&C"]);
      programmeren_female.push(entry["Programmeren"]);
      m_en_i_female.push(entry["M&I"]);
      mediageschiedenis_female.push(entry["Mediageschiedenis"]);
      business_mapping_female.push(entry["Business Mapping"]);
      plug_and_play_female.push(entry["Plug & Play"]);
      slc_female.push(entry["SLC"]);
    }

  });

  var ucd_male_total = 0;
  $.each(ucd_male, function(index,item) {
    ucd_male_total += parseInt(item);
  });
  ucd_male_average = ucd_male_total/ucd_male.length;

  var ucd_female_total = 0;
  $.each(ucd_female, function(index,item) {
    ucd_female_total += parseInt(item);
  });
  ucd_female_average = ucd_female_total/ucd_female.length;

  console.log(ucd_male);
  console.log(ucd_female);  

});

Below the structure of rawdata (this is one entry):

[{"id":1,"geslacht":"Vrouw","totaal punten":60,"project      1":9,"ucd":6,"internetstandaarden":7,"understanding design":7,"Teamproject deel 1":6,"Taal in context":6,"Vormgeving":7,"Marketing":6,"Students in Motion":7,"Teamproject deel 2":7,"H&C":6,"Programmeren":7,"M&I":7,"Project 3":6,"Mediageschiedenis":6,"Business Mapping":7,"Plug & Play":7,"SLC":7,"Studieregie":8,"plaats":"JOPPE","geboortejaar":1990,"vooropleiding":"HAVO","vooropleiding afgerond in":2009},

Upvotes: 1

Views: 317

Answers (3)

charlietfl
charlietfl

Reputation: 171669

You have several critical typos in your code

 $.each(rawdata, function(i, entry) {
    if(entry["geslacht"] == "Man") {
      array_one.push(entry["key"]);
    else {
      array_one.push(entry["key"]); /* <===== should be array_two*/
    }
  )};

var array_one_total = 0;
$.each(array_one, function() {
  array_one += this;/* <===== should be array_one_total*/
});

Upvotes: 1

Magus
Magus

Reputation: 15104

It's looks like your code contains some typo problem.

var array_one_total = 0;
$.each(array_one, function() {
  array_one += this;
});

It just can't works. I think you wanted to do that :

var array_one_total = 0;
$.each(array_one, function(index, value) {
  array_one_total += value;
});

And you talks about an array_two but i see only one code line about this array_two. So i don't get how array_twocan contains anything.

Upvotes: 2

Behnam Esmaili
Behnam Esmaili

Reputation: 5967

it seems problem is with this. try following instead.

$.each(array_one, function(index,item) {
     array_one += parseInt(item);
});

also check to see if array_one is NAN before division.

Upvotes: 0

Related Questions