为了账号安全,请及时绑定邮箱和手机立即绑定

forEach 通过对象循环并使用 document.write() 打印;

forEach 通过对象循环并使用 document.write() 打印;

开满天机 2021-10-14 14:21:06
有一个数组,由我需要遍历的几个对象组成(最好使用 forEach 循环,这是练习的范围)。我需要在屏幕上打印它们的详细信息(不能只在控制台上看到它们,这会更直接),并且理想情况下,有工具可以用 CSS 格式化它们。通过实际的 forEach 循环(见下文,try-1),我可以更接近地打印一个完整的 [object Object] 对序列,而无法更深入地检索数据。我设法打印了每个对象的详细信息(try-2),但只能通过手动调用每个属性的繁琐列表。有没有办法循环遍历对象并以有序的方式检索其数据并将其打印在屏幕上?提前致谢var bulbasaur = { name: "Bulbasaur", height: 0.7, weight: 6.9, hatchSteps: 5100, types: ["Grass", " Poison"], eggGroups: ["Monster", " Grass"], abilities: ["Chlorophyll", " Overgrow"]};var ivysaur = { name: "Ivysaur", height: 1, weight: 13, hatchSteps: 5100, types: ["Grass", " Poison"], eggGroups: ["Monster", " Grass"], abilities: ["Chlorophyll", " Overgrow"]};var venusaur = { name: "Venusaur", height: 2, weight: 100, hatchSteps: 5100, types: ["Grass", " Poison"], eggGroups: ["Monster", " Grass"], abilities: ["Chlorophyll", " Overgrow"]};var charmander = { name: "Charmander", height: 0.6, weight: 8.5, hatchSteps: 5100, types: ["Fire"], eggGroups: ["Monster", " Dragon"], abilities: ["Blaze", " Solar-power"]};var charmeleon = { name: "Charmeleon", height: 1.1, weight: 19, hatchSteps: 5100, types: ["Fire"], eggGroups: ["Monster", " Dragon"], abilities: ["Blaze", " Solar-power"]};var charizard = { name: "Charizard", height: 1.7, weight: 90.5, hatchSteps: 5100, types: ["Fire", " Flying"], eggGroups: ["Monster", " Dragon"], abilities: ["Blaze", " Solar-power"]};var squirtle = { name: "Squirtle", height: 0.5, weight: 9, hatchSteps: 5100, types: ["Water"], eggGroups: ["Monster", " Water 1"], abilities: ["Rain-dish", " Torrent"]};var wartortle = { name: "Wartortle", height: 1, weight: 22.5, hatchSteps: 5100, types: ["Water"], eggGroups: ["Monster", " Water 1"], abilities: ["Rain-dish", " Torrent"]};var repository = [  bulbasaur,  ivysaur,  venusaur,  charmander,  charmeleon,  charizard,  squirtle,  wartortle];(try-1)const keys = Object.entries(repository)keys.forEach(function(item, key) {  document.write(repository[key] + '<br>');  document.write(item);});```
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

查看实际数据的一种快速方法是JSON.stringify将数据转换为字符串。


var bulbasaur = { name: "Bulbasaur", height: 0.7, weight: 6.9, hatchSteps: 5100, types: ["Grass", " Poison"], eggGroups: ["Monster", " Grass"], abilities: ["Chlorophyll", " Overgrow"]};

var ivysaur = { name: "Ivysaur", height: 1, weight: 13, hatchSteps: 5100, types: ["Grass", " Poison"], eggGroups: ["Monster", " Grass"], abilities: ["Chlorophyll", " Overgrow"]};

var venusaur = { name: "Venusaur", height: 2, weight: 100, hatchSteps: 5100, types: ["Grass", " Poison"], eggGroups: ["Monster", " Grass"], abilities: ["Chlorophyll", " Overgrow"]};

var charmander = { name: "Charmander", height: 0.6, weight: 8.5, hatchSteps: 5100, types: ["Fire"], eggGroups: ["Monster", " Dragon"], abilities: ["Blaze", " Solar-power"]};

var charmeleon = { name: "Charmeleon", height: 1.1, weight: 19, hatchSteps: 5100, types: ["Fire"], eggGroups: ["Monster", " Dragon"], abilities: ["Blaze", " Solar-power"]};

var charizard = { name: "Charizard", height: 1.7, weight: 90.5, hatchSteps: 5100, types: ["Fire", " Flying"], eggGroups: ["Monster", " Dragon"], abilities: ["Blaze", " Solar-power"]};

var squirtle = { name: "Squirtle", height: 0.5, weight: 9, hatchSteps: 5100, types: ["Water"], eggGroups: ["Monster", " Water 1"], abilities: ["Rain-dish", " Torrent"]};

var wartortle = { name: "Wartortle", height: 1, weight: 22.5, hatchSteps: 5100, types: ["Water"], eggGroups: ["Monster", " Water 1"], abilities: ["Rain-dish", " Torrent"]};


var repository = [

  bulbasaur,

  ivysaur,

  venusaur,

  charmander,

  charmeleon,

  charizard,

  squirtle,

  wartortle

];


document.querySelector('#repository').textContent = JSON.stringify(repository,null,2);

<pre id="repository"></pre>

如果你想生成 HTML,使用像Mustache / Handlebars这样的模板引擎可能更简单/更强大


查看完整回答
反对 回复 2021-10-14
  • 1 回答
  • 0 关注
  • 238 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信