【问题标题】:In a key value pair how to print out a value that isn't null JavaScript在键值对中如何打印出一个非空值 JavaScript
【发布时间】:2019-04-07 23:04:51
【问题描述】:

因此,在这样的示例中,我试图打印出“信息”中不包含 null 的名称

let files = [
{
  name: 'untitled',
  information: null
},
{
  name: 'folder'
  information: 'has storage'
},
{
  name: 'new folder',
  information: 'has 42 items'
},

我一直在尝试使用的代码是这个,但是当我尝试打印出不包含 null 的文件夹的名称时它不起作用

let info = files.filter((a) => {
  if (a.information !== null )
  return a
  });
  
  console.log(info)

当我输入console.log(info.length) 以查看它是否真的在接收时,有多少项目中没有空值。它确实计算了项目,但是当我尝试查看是否可以打印出他们的名字时,它只打印undefined

还有其他方法吗?

【问题讨论】:

  • 那么你为什么不检查你的information 键呢?喜欢const info = files.sort(a => a.information !== null)
  • 等一下,现在这个问题与我回答的问题截然不同......
  • 现在我很困惑问题出在哪里 - 你的代码有效。或者应该工作 - 它正确过滤东西。然而你声称它正在打印undefined - 什么时候?你在做什么来得到它,因为那将是有问题的代码。
  • 它有效,但我一直在尝试做的只是打印出每个不包含 null 的文件夹的名称。
  • 但是如何你想打印那个?过滤器再次起作用,您可以从中获得正确的输出。如果您看到的是 undefined,它与您发布的代码无关。

标签: javascript arrays null key-value


【解决方案1】:

filter 返回一个数组,如果要打印名称,则需要再次遍历数组

let files = [{
    name: 'untitled',
    information: null
  },
  {
    name: 'folder',
    information: 'has storage'
  },
  {
    name: 'new folder',
    information: 'has 42 items'
  }
]

let info = files.filter((a) => {
  return a.information !== null;
}).forEach(function(item) {
  console.log(item.name)
})

【讨论】:

    【解决方案2】:

    如果您只想要文件夹的名称。使用 map 来提取它,如下所示:

    let files = [{
        name: 'untitled',
        information: null
      },
      {
        name: 'folder',
        information: 'has storage'
      },
      {
        name: 'new folder',
        information: 'has 42 items'
      }
    ]
    
    let info = files.filter((a) => {
      return a.information !== null;
    }).map((item)=> {
    // Get only the names of the folder
      return item.name;
    });
    
    console.log(info);

    【讨论】:

      【解决方案3】:

      如果有帮助,请尝试以下代码。

             let files = [
              {
                name: 'untitled',
                information: null
              },
              {
                name: 'folder'
                information: 'has storage'
              },
              {
                name: 'new folder',
                information: 'has 42 items'
              };
      
          var getFilesWithInfo = function () {
              var array = [];
              for(var i=0; i<files.length;i++){
                 if(files[i].information){
                   array.push(files[i]
                }
                return array;
              }
          }
      
      console.log(getFilesWithInfo());
      

      【讨论】:

        【解决方案4】:

        您可以通过 filtermap 的两个简单步骤完成此操作。

        const files = [{
            name: 'untitled',
            information: null
          },
          {
            name: 'folder',
            information: 'has storage'
          },
          {
            name: 'new folder',
            information: 'has 42 items'
          }
        ];
        
        const fileNames = files
          .filter(f => f.information !== null)
          .map(f => f.name);
        
        console.log(fileNames);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-06-08
          • 2016-05-13
          • 1970-01-01
          • 1970-01-01
          • 2020-08-22
          • 2018-04-23
          • 1970-01-01
          相关资源
          最近更新 更多