【问题标题】:Representing blank spaces in an array or Adding a Holder for blank spaces in an array表示数组中的空格或为数组中的空格添加 Holder
【发布时间】:2021-02-04 11:18:50
【问题描述】:

我有一个元素数组(名称列表)和他们的投票。因此,如果有人在场并投票,则会在此人姓名旁边显示 Y、N、A。但当此人不在场并因此未投票时,它将显示为空白。我想知道如何表示数组中的空白字段。
因为会发生这样的情况,当读取数组时(假设为 John,它将分配 Y、N、Y、Y 并填充空白点)。我想要一个空白点的支架,这样它就可以显示为 Y、N、,
,Y,Y。而且还是能看到空格。谢谢。
示例:

【问题讨论】:

    标签: arrays mongodb spaces


    【解决方案1】:

    您可以使用与undefined 不同的null 值。

    一个基本的代码示例:

    list = [
      { name: "Jessica", votes: ['Y', 'Y', 'Y', 'Y', null, 'A'] }
      /* ... */
    ];
    
    for (let people of list)
    {
       for (let vote of people.votes)
       {
          if (vote === null) console.log(`${people.name} was not present`);
          else console.log(`${people.name} vote ${vote}`);
       }
    }
    

    考虑使用三元组=== 进行比较,所以undefinednull0false 是不同的。

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 2022-10-16
      相关资源
      最近更新 更多