首页 > 解决方案 > JSON 文件中的随机对象

问题描述

所以我有一个名为 pokemon.json 的文件,它是一个相当大的 pokemon 列表及其所有统计数据。我正在尝试从 json 文件中读取数据,生成一个随机数,然后发送 pokemon 的信息,谁的 id 与随机数匹配。

目前我正在尝试这个


var rawdata = fs.readFileSync('pokemon.json');
  var pokemon2 = JSON.parse(rawdata);

  var randomNum = Math.round(Math.random() * (1 + 1169) - 1);

  var pokemon = pokemon2[randomNum];

  console.log(pokemon);
  console.log(typeof(pokemon));

  res.json(pokemon);

我尝试使用 for 循环遍历数据,它似乎只给了我大约四分之一时间的可用数据。我认为这与在尝试找到随机数据之前没有所有数据有关,但我不确定如何解决这个问题。

JSON的前几行

[
    {"id":1,"name":"Bulbasaur","stage":1,"galar_dex":"foreign","base_stats":[45,49,49,65,65,45],"ev_yield":[0,0,0,1,0,0],"abilities":["Overgrow","Overgrow","Chlorophyll"],"types":["Grass","Poison"],"items":[],"exp_group":"MediumSlow","egg_groups":["Monster","Grass"],"hatch_cycles":20,"height":0.7,"weight":6.9,"color":"Green","level_up_moves":[[1,"Tackle"],[1,"Growl"],[3,"Vine Whip"],[6,"Growth"],[9,"Leech Seed"],[12,"Razor Leaf"],[15,"Poison Powder"],[15,"Sleep Powder"],[18,"Seed Bomb"],[21,"Take Down"],[24,"Sweet Scent"],[27,"Synthesis"],[30,"Worry Seed"],[33,"Double-Edge"],[36,"Solar Beam"]],"egg_moves":["Skull Bash","Petal Dance","Curse","Ingrain","Nature Power","Toxic"],"tms":[10,11,17,19,21,24,25,28,29,31,34,39,41,46,50,74,76,88,94],"trs":[0,1,17,20,22,26,27,59,65,71,72,77,85],"evolutions":[{"species":"Ivysaur-0","method":"LevelUp","method_value":16}],"description":"While it is young, it uses the nutrients that are stored in the seed on its back in order to grow."},
    {"id":2,"name":"Ivysaur","stage":2,"galar_dex":"foreign","base_stats":[60,62,63,80,80,60],"ev_yield":[0,0,0,1,1,0],"abilities":["Overgrow","Overgrow","Chlorophyll"],"types":["Grass","Poison"],"items":[],"exp_group":"MediumSlow","egg_groups":["Monster","Grass"],"hatch_cycles":20,"height":1,"weight":13,"color":"Green","level_up_moves":[[1,"Tackle"],[1,"Growl"],[1,"Vine Whip"],[1,"Growth"],[9,"Leech Seed"],[12,"Razor Leaf"],[15,"Poison Powder"],[15,"Sleep Powder"],[20,"Seed Bomb"],[25,"Take Down"],[30,"Sweet Scent"],[35,"Synthesis"],[40,"Worry Seed"],[45,"Double-Edge"],[50,"Solar Beam"]],"egg_moves":[],"tms":[10,11,17,19,21,24,25,28,29,31,34,39,41,46,50,74,76,88,94],"trs":[0,1,17,20,22,26,27,59,65,71,72,77,85],"evolutions":[{"species":"Venusaur-0","method":"LevelUp","method_value":32}],"description":"Exposure to sunlight adds to its strength. Sunlight also makes the bud on its back grow larger."},
...
....
....
....
...
{"id":1170,"name":"Eternatus 1","stage":3,"galar_dex":"400","base_stats":[255,115,250,125,250,130],"ev_yield":[3,0,0,0,0,0],"abilities":["Pressure","Pressure","Pressure"],"types":["Poison","Dragon"],"items":[],"exp_group":"Slow","egg_groups":["Undiscovered"],"hatch_cycles":120,"height":100,"weight":0,"color":"Purple","level_up_moves":[[1,"Poison Tail"],[1,"Confuse Ray"],[1,"Dragon Tail"],[1,"Agility"],[8,"Toxic"],[16,"Venoshock"],[24,"Dragon Dance"],[32,"Cross Poison"],[40,"Dragon Pulse"],[48,"Flamethrower"],[56,"Dynamax Cannon"],[64,"Cosmic Power"],[72,"Recover"],[80,"Hyper Beam"],[88,"Eternabeam"]],"egg_moves":[],"tms":[6,8,9,11,16,17,18,21,24,25,26,39,57,58,73,74,76,92,97],"trs":[2,12,20,22,26,27,33,44,51,54,57,62,70,78,91],"evolutions":[],"description":null}
]
    

为了补充说明,我运行了 3 次代码。以下是控制台记录的响应

undefined
[0] undefined
[0] undefined
[0] undefined
[0] {
[0]   id: 606,
[0]   name: 'Beheeyem',
[0]   stage: 2,
[0]   galar_dex: '278',
[0]   base_stats: [ 75, 75, 75, 125, 95, 40 ],
[0]   ev_yield: [ 0, 0, 0, 2, 0, 0 ],
[0]   abilities: [ 'Telepathy', 'Synchronize', 'Analytic' ],
[0]   types: [ 'Psychic' ],
[0]   items: [],
[0]   exp_group: 'MediumFast',
[0]   egg_groups: [ 'HumanLike' ],
[0]   hatch_cycles: 20,
[0]   height: 1,
[0]   weight: 34.5,
[0]   color: 'Brown',
[0]   level_up_moves: [
[0]     [ 1, 'Psychic Terrain' ],
[0]     [ 1, 'Confusion' ],
[0]     [ 1, 'Growl' ],
[0]     [ 1, 'Imprison' ],
[0]     [ 1, 'Teleport' ],
[0]     [ 18, 'Psybeam' ],
[0]     [ 24, 'Power Split' ],
[0]     [ 24, 'Guard Split' ],
[0]     [ 30, 'Headbutt' ],
[0]     [ 36, 'Zen Headbutt' ],
[0]     [ 45, 'Recover' ],
[0]     [ 52, 'Calm Mind' ],
[0]     [ 60, 'Wonder Room' ],
[0]     [ 68, 'Psychic' ]
[0]   ],
[0]   egg_moves: [],
[0]   tms: [
[0]      8,  9, 14, 16, 17, 18, 19, 21,
[0]     22, 23, 24, 25, 30, 31, 33, 39,
[0]     44, 48, 60, 61, 70, 71, 76, 91
[0]   ],
[0]   trs: [
[0]      8, 11, 12, 19, 20, 25, 26,
[0]     27, 33, 34, 35, 38, 40, 44,
[0]     49, 58, 65, 68, 69, 70, 82,
[0]     83
[0]   ],
[0]   evolutions: [],
[0]   description: 'Sometimes found drifting above wheat fields, this Pokémon can control the memories of its opponents.'
[0] }
[0] object

标签: node.jsfs

解决方案


修复你如何选择随机口袋妖怪

   var randomNum = Math.round(Math.random() * (1 + 1169) - 1);

-> var randomNum = Math.floor(Math.random() * pokemon2.length);

推荐阅读