简体   繁体   English

如何使用javascript中创建的对象在html中创建表?

[英]How to create a table in html using an object created in javascript?

I've started working with web development, specifically working with JavaScript and node.js, and created a web scraper. 我已经开始进行Web开发,特别是使用JavaScript和node.js,并创建了Web抓取工具。 Currently, it takes data from http://www.dotamax.com (stats page for the video game DOTA 2) and creates an object which contains win rate, pick rate, and an arbitrary score that the code assigns to each hero. 当前,它从http://www.dotamax.com (视频游戏DOTA 2的统计信息页面)中获取数据,并创建一个对象,其中包含获胜率,选择率和代码分配给每个英雄的任意得分。 The JavaScript works, but now I'm lost in using the code inside a webpage. JavaScript可以运行,但是现在我迷失了在网页内使用代码的机会。 I created a local web server on my machine so I could use the JavaScript file within an html file but I'm not sure what is the best to go about creating a table inside html using the object in the JavaScript. 我在机器上创建了本地Web服务器,因此可以在html文件中使用JavaScript文件,但是我不确定使用JavaScript中的对象在html中创建表的最佳方法。 Any sort of direction would be really helpful! 任何方向都将非常有帮助! Here's the code for my JavaScript file, the object I want to put in a table is heroObjs. 这是我的JavaScript文件的代码,我要放在表中的对象是heroObjs。

var request = require('request');
var cheerio = require('cheerio');

// list that will iterate through every skill level. used in the url
skillLevels = {
  'Very high': 'vh',
  'High': 'h',
  'Normal': 'n'
};

heroes = ['pudge', 'windrunner', 'nevermore', 'earthshaker', 'lina','pudge',
   'queenofpain', 'invoker', 'antimage', 'juggernaut', 'alchemist',
   'doom_bringer', 'tusk', 'rubick', 'bounty_hunter', 'slark',
   'storm_spirit', 'ember_spirit', 'spectre', 'spirit_breaker', 'silencer',
   'legion_commander', 'lion', 'phantom_assassin', 'mirana', 'zuus',
   'undying', 'rattletrap', 'templar_assassin', 'bloodseeker',
   'witch_doctor', 'crystal_maiden', 'gyrocopter', 'dazzle','winter_wyvern',
   'necrolyte', 'ancient_apparition', 'ogre_magi', 'skeleton_king',
   'phantom_lancer', 'axe', 'magnataur', 'tiny', 'riki', 'slardar',
   'earth_spirit', 'leshrac', 'furion', 'sand_king', 'omniknight',
   'huskar', 'disruptor', 'tinker', 'ursa', 'bristleback', 'sniper',
   'venomancer', 'nyx_assassin', 'life_stealer', 'clinkz', 'vengefulspirit',
   'skywrath_mage', 'kunkka', 'lich', 'faceless_void', 'dark_seer', 'techies',
   'jakiro', 'abaddon', 'phoenix', 'sven', 'shadow_shaman', 'luna', 'viper',
   'enigma', 'shredder', 'weaver', 'tidehunter', 'night_stalker', 'medusa',
   'chaos_knight', 'puck', 'drow_ranger', 'centaur', 'keeper_of_the_light',
   'pugna', 'dragon_knight', 'warlock', 'morphling', 'broodmother','meepo', 'terrorblade', 'treant', 'razor', 'bane', 'batrider',
   'death_prophet', 'troll_warlord', 'wisp', 'shadow_demon', 'naga_siren',
   'obsidian_destroyer', 'enchantress', 'lone_druid', 'beastmaster',
   'lycan', 'oracle', 'brewmaster', 'elder_titan', 'visage', 'chen'
   ];

// creates a multidimensional object that holds hero stats
var heroObjs = {};
for (var i = 0; i < heroes.length; i++) {
   var hero = heroes[i];
   if (hero in heroObjs == false) {
     heroObjs[hero] = {
       "Very high": {
         "Pick Rate": null,
         "Win Rate": null,
         "Score": null
       },
       "High": {
         "Pick Rate": null,
         "Win Rate": null,
         "Score": null
       },
       "Normal": {
         "Pick Rate": null,
         "Win Rate": null,
         "Score": null
       },
     };
   }    
 }

for (skills in skillLevels) {
   var url = "http://www.dotamax.com/hero/played/?skill=" + skillLevels[skills];
   request(url, (function (skills) {
     return function (err, resp, body) {
       if (err)
         throw err;
        $ = cheerio.load(body);
       //creates the score and gets win rate and pick rate from dotamax.com
       //currently only fills score for very high skill bracket
       $("tbody tr").each(function (hero) {
         $(this).find('td div:contains("%")').each(function () {

           var containsPickRate = $(this).parent().parent().find("td").eq(1).toString();
      var containsHeroName = $(this).parent().parent().toString();
      var pickRateIndex = containsPickRate.indexOf('10px">') + 6;
      var endPickRateIndex = containsPickRate.indexOf('</div>');
      var heroNameIndex = containsHeroName.indexOf("detail/") + 7;
      var endHeroNameIndex = containsHeroName.indexOf('&apos;)" style');
      var heroName = containsHeroName.substring(heroNameIndex, endHeroNameIndex);
      var pickRate = containsPickRate.substring(pickRateIndex, endPickRateIndex);
      pickRate = pickRate.split(',').join("");
      var winRate = parseFloat($(this).text());
      pickRate = parseFloat(pickRate);
      if (heroName in heroObjs == true) {
        heroObjs[heroName][skills]["Pick Rate"] = pickRate;
        heroObjs[heroName][skills]["Win Rate"] = winRate;
        if (skills === "Very high") {
          var winRateCont = 1.4 * .01 * Math.pow(winRate, 2)
          var pickRateCont = .8 * ((10775 / (100 + 1090 * Math.pow(Math.E, -.000005 * pickRate))) - 8.3)
          heroObjs[heroName][skills]["Score"] = (pickRateCont + winRateCont)
        }
      }
    })
  });
}
   })(skills));
 }

 //waits 10 seconds to display heroObjs which is approximate time 
 //it takes for the previous function to fill heroObjs
 setTimeout(function () {
    console.log(heroObjs);
  }, 10000);

First off, your best choice is to use an established Javascript framework that does this for you. 首先,最好的选择是使用已建立的Javascript框架为您执行此操作。 For instance, jQuery and/or angular.js. 例如,jQuery和/或angular.js。

For instance, with angular.js, you would simply do something like: 例如,使用angular.js,您只需执行以下操作:

<table>
  <tr data-ng-repeat="hero in heroObjs">
    <td>{{hero.attr1}}</td>
  </tr>
</table>

Angular will take care of the rest. Angular将负责其余的工作。 It will parse your data, and build the html for you. 它将解析您的数据,并为您构建html。 It takes a while to understand a framework like angular or jQuery, but the time is well spent. 理解Angular或jQuery之类的框架需要花费一些时间,但是花费的时间却很充裕。 Maintaining your own library of functions to do anything similar is a big waste of time, and you will spend more time debugging than building. 维护自己的函数库以执行类似操作会浪费大量时间,并且调试将比构建花费更多的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM