1

我正在尝试从 JSON 提要创建 2 个数组,然后计算两者的平均值。首先,我将数组的方式是这样的:

$.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"]);
    }
  )};
)};

这很好用,除非我查看我的控制台array_one似乎是某种对象数组,而 `array_two 似乎是一个普通数组。因此,当我尝试像这样计算两者的平均值时:

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

array_one_average = array_one_total/array_one.length;

我得到一个正确和错误的价值。array_one在我的控制台中像这样记录“NaN”,而 array_two 显示正确的平均值:

Javascript奇怪的结果在控制台

这似乎是数组格式的问题,但我不明白为什么返回第二个值是正确的,因为我对它们都使用了相同的方法。

如果我将两个数组都直接记录到控制台会发生什么,您可以看到它们的不同显示方式:

控制台日志

好吧,我收到很多回复,要求我提供整个代码,数组的数量使其更难阅读,但这里是:

$.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);  

});

下面的结构rawdata(这是一个条目):

[{"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},
4

3 回答 3

2

看起来您的代码包含一些拼写错误。

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

它只是行不通。我想你想这样做:

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

你谈到了一个,array_two但我只看到一个关于这个的代码行array_two。所以我不明白怎么array_two能包含任何东西。

于 2012-11-24T17:48:02.730 回答
1

您的代码中有几个严重的拼写错误

 $.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*/
});
于 2012-11-24T17:50:25.340 回答
0

似乎问题出在this. 尝试跟随。

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

还要在除法之前检查 array_one 是否为 NAN。

于 2012-11-24T17:46:46.960 回答