logging - js控制台日志字符串格式化起点
问题描述
let amounts = [1,10,100]
let animals = [cat,dog,horse]
for (let n = 0; n<3; n++){
console.log("I have " + amounts[n] + " of " + animals[n] + " in total" )
}
目前我得到:
I have 1 cat in total
I have 10 dog in total
I have 100 horse in total
有没有办法用“列”格式化这个日志(它们是一个一个传入的,我不能将它们记录为表格)
I have 1 cat in total
I have 10 dog in total
I have 100 horse in total
解决方案
let amounts = [1, 10, 100]
let animals = ['cat', 'dog', 'horse']
for (let n = 0; n < 3; n++){
console.log("I have\t" + amounts[n] + "\t" + animals[n] + "\tin total" )
}
另一种解决方案是使用Array.forEach()
let amounts = [1, 10, 100]
let animals = ['cat', 'dog', 'horse']
amounts.forEach((amount, index) => {console.log(`I have\t${amount}\t${animals[index]}\tin total`)})
推荐阅读
- java - 远程服务器对 Spring Boot Controller 的响应
- scala - 使用依赖类型扩展
- python - 在 Python 列表中对一组相似的数字进行分组
- html - 为什么谷歌图片链接让我进入谷歌搜索
- mysql - Symfony - 如何在实体中添加字段类型年份
- graphql - SilverStripe 4 GraphQL 和 GraphiQL UI 无法正常工作,即使从文档中正确遵循了程序
- python - 在 Flask Python 中使用全局变量时不支持的操作数类型
- python - 无法从 shapefile 中的日期属性中提取季节
- javascript - 如何在Vue观察者中获取数组值?
- python - 合并 CSV 文件并为每个文件添加列