【问题标题】:Is there a way to use items in an array as booleans? Node.js有没有办法将数组中的项目用作布尔值?节点.js
【发布时间】:2021-11-11 22:39:33
【问题描述】:

我正在使用 node.js 开发地牢爬虫。我想要一种优雅的方式来跟踪用户的结局。我在想,如果我可以拥有每个结局的数组,然后如果实现了结局,只需将一个项目设置为 true,那就太好了。这样可以轻松浏览列表并打印已实现的结局,然后计算完成百分比。

我的尝试:

var end1 = true
var end2 = true
var end3 = false
var end4 = true
var end5 = false

var endings = [end1, end2, end3, end4, end5]

function listEndings() {
  console.log("You have found these endings:")
  var total = 0

  for (let i = 0; i < endings.length; i++) {
    if (endings[i] = true) {
      console.log(String(endings[i]))
      total = total + 1
    }
  }

  console.log(`\nIn total you have ${total}/${endings.length + 1} endings. (${(total/(endings.length + 1))*100}%)`)
}

我想要的输出

You have found these endings:
end1
end2
end4

In total you have 3/5 endings. (60%)

可以使用这样的方法吗?有没有其他推荐的方法?

非常感谢您的帮助!

【问题讨论】:

  • 我会将结尾推送到像 archivedArr.push(endings[3]) 等这样的秒数组,而不是最后不需要“大”for循环。也不需要真假的东西
  • 或者只是将它们存储在单个值中,例如没有结尾 00000、所有结尾 11111、end3 00100 ...
  • (endings[i] = true 是赋值而不是比较。为了比较使用===。

标签: javascript node.js arrays boolean


【解决方案1】:

在这种情况下,声明包含对象的数组变量。因此,您的数据是灵活的。如果需要,您可以随时添加数据。

使用过滤器和映射函数来创建这样的输出

var endings = [
{"no": 1,"bool": true},
{"no": 2,"bool": true},
{"no": 3,"bool": false},
{"no": 4,"bool": true},
{"no": 5,"bool": false},
{"no": 6,"bool": true},
{"no": 7,"bool": false}
]

var result = endings.filter((a)=>a.bool==true)
var resultMsg = result.map((a)=> `end${a.no}\n`)
var resultVal = parseInt((result.length/endings.length)*100)

console.log(`You have found these endings:\n ${resultMsg}`)
console.log(`\nIn total you have ${result.length}/${endings.length} endings. (${resultVal}%)`)

【讨论】:

    【解决方案2】:

    使用object 而不是array 并使用Object.entries 循环key 和value 来获得所需的结果会简单直接。

    var end1 = true;
    var end2 = true;
    var end3 = false;
    var end4 = true;
    var end5 = false;
    
    var endings = { end1, end2, end3, end4, end5 };
    
    function listEndings() {
      let count = 0, entries = Object.entries(endings);
      
      console.log("You have found these endings:");
      for (let [k, v] of entries) {
        if (v) {
          console.log(`${k}`);
          ++count;
        }
      }
    
      console.log(
        `In total you have ${count}/${entries.length} endings. (${
          (count * 100) / entries.length
        }%)`
      );
    }
    
    listEndings();

    【讨论】:

      【解决方案3】:

      我认为最好将这些值存储为一个数组来存储玩家已经找到的结局:

      let endingsFound = []
      const totalEndings = 5
      //(as a side note, better use let than var)
      
      console.log('Endings found')
      //suppose we find endings three and four
      endingsFound.push(3)
      endingsFound.push(4)
      
      endingsFound.forEach((end) => console.log(`Ending ${end}`))
      
      console.log(`In total you have found ${endingsFound.length +1}/${totalEndings}`);
      

      如果您仍决定将其存储为布尔数组,请使用以下函数:

      const indices = arr.reduce(
          (out, bool, index) => bool ? out.concat(index) : out, 
          []
        )
      

      正如here 所介绍的那样,获取找到的结尾数组,然后执行相同操作。

      【讨论】:

        猜你喜欢
        • 2014-03-20
        • 2019-03-03
        • 2020-01-11
        • 2017-02-07
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        相关资源
        最近更新 更多