未捕获的TypeError:无法读取app.js上未定义的属性'push':57

时间:2017-10-11 06:31:44

标签: javascript

如何修复错误未捕获的TypeError:无法在app.js上读取未定义的属性'push':57

我的文件js app.js包含行代码:

    url = 'https://siayi.github.io/website-desa/sites.json';
$.getJSON(url, function (json) {
    count = new WebsiteCounter(json);
    $("#total").html(count.count);
    $("#kecamatan").html(count.kecamatan);
    $("#kabkot").html(count.kabkot);
    console.log(count);
    $(".render-bar").toggle();
    tables = new WebsiteTable(json);
    $.each(tables, function(key,table) {
      $("#tables").append(table);
    });
});


function WebsiteTable(data) {
  var tables = [];
  var headers = ["Desa", "Kecamatan", "Kabupaten/Kota"];
  var formatted = {
  "Aceh": [],
  "Sumatera Utara": [],
  "Sumatera Barat": [],
  "Riau": [],
  "Papua Barat": []
  };

  $.each(data, function(key,site) {
    formatted[site.provinsi].push([site.title, site.kecamatan, site.kabkot, site.url])
  });

1 个答案:

答案 0 :(得分:0)

您正在尝试将元素推送到Object中,这是不可能的,尝试在推入数组之前声明数组并使用for(of)循环。



    var url = 'https://siayi.github.io/website-desa/sites.json';
$.getJSON(url, function (json) {
    var tables = new WebsiteTable(json);
});


function WebsiteTable(data) {
  var tables = [];
  var headers = ["Desa", "Kecamatan", "Kabupaten/Kota"];
  var formatted = {
  "Aceh": [],
  "Sumatera Utara": [],
  "Sumatera Barat": [],
  "Riau": [],
  "Papua Barat": [],
   site: []
  };
  
  for(var site of data){
    var obj = {
      kabkot: site.kabkot,
      kecamatan: site.kecamatan,
      provinsi: site.provinsi,
      title: site.title,
      url: site.url
    }
    
    formatted.site.push(obj)
   
  }
  
  console.log(formatted.site)
  
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;