简体   繁体   English

JavaScript过滤器方法返回过滤后的数组和空数组

[英]JavaScript filter method returning filtered array and empty array

I am trying to log the current reading status but instead of getting just the log I am also getting an empty array when calling displayInformation(). 我正在尝试记录当前的阅读状态,但不仅仅是获取日志,而是在调用displayInformation()时得到一个空数组。 How can I get the result I want without an additional empty array? 在没有其他空数组的情况下如何获得所需的结果? and why is it returning an empty array? 为什么返回一个空数组?

function displayInformation() {
  function statusRead(obj){
    if (obj.readingStatus === false) {
      console.log('You still need to read ' + obj.title + ' by ' + obj.author + '.');
    } else {
      console.log('Already read ' + obj.title + ' by' + obj.author + '.');
    }
  }

  var statusFilter = library.filter(statusRead);
  console.log(statusFilter);
}

var library = [ 
    {
        title: 'Bill Gates',
        author: 'The Road Ahead',
        readingStatus: true
    },
    {
        title: 'Steve Jobs',
        author: 'Walter Isaacson',
        readingStatus: true
    },
    {
        title: 'Mockingjay: The Final Book of The Hunger Games',
        author: 'Suzanne Collins',
        readingStatus: false
    }
];

displayInformation();

When you invoke displayInformation() this is whats logged to console 当您调用displayInformation()时,这是记录到控制台的内容

"Already read Bill Gates byThe Road Ahead."
"Already read Steve Jobs byWalter Isaacson."
"You still need to read Mockingjay: The Final Book of The Hunger Games by Suzanne Collins."
[]

How can I get the result I want without an additional empty array? 在没有其他空数组的情况下如何获得所需的结果?

You have to use .forEach() or a normal for loop to do what you want to do. 您必须使用.forEach()或常规的for loop来执行您想要的操作。 .filter() use case is entirely different from yours. .filter()用例与您的用例完全不同。

why is it returning an empty array? 为什么返回一个空数组?

Since the .filter() method will return a filtered array, it is returning an empty array in your case as your callBack function is returning undefined all the time. 由于.filter()方法将返回过滤后的数组,因此在您的情况下,它将返回一个空数组,因为callBack函数始终都返回undefined


Your code should have to be like this, 您的代码必须像这样,

function displayInformation(library) {
 library.forEach(function(obj){
    if (obj.readingStatus === false) {
      console.log('You still need to read ' + obj.title + ' by ' + obj.author + '.'); 
    } else { 
      console.log('Already read ' + obj.title + ' by' + obj.author + '.'); 
    }
 });
}

displayInformation(library);

The pure for-loop version, 纯for循环版本,

function displayInformation(library) {
  var i = 0, len = library.length, obj;
  for (; i < len; i++) {
    obj = library[i];
    if (obj.readingStatus === false) {
      console.log('You still need to read ' + obj.title + ' by ' + obj.author + '.');
    } else {
      console.log('Already read ' + obj.title + ' by' + obj.author + '.');
    }
  }
}

displayInformation(library);

That's because you're printing out statusFilter which is the result of filtering using statusRead . 那是因为您要打印出statusFilter ,这是使用statusRead进行过滤的结果。 Since statusRead never returns true , the result will be empty. 由于statusRead从不返回true ,结果将为空。 The way that filter works is it will create a new array from an old array, where it includes every value that returns truthy. filter工作方式是从旧数组创建一个新数组,其中包含返回真值的每个值。 For example, here's how you could get all of the even numbers from a list of numbers. 例如,这是从数字列表中获取所有偶数数字的方法。

var evens = numbers.filter(function(x) {
  return x % 2 === 0;
});

So, again, since you are never returning true from your filter predicate, you're getting an empty list which you proceed to console.log out. 因此,再次,由于您永远不会从filter谓词返回true ,因此您会得到一个空列表,然后继续进行console.log注销。

To just iterate through a list, you should either use a for loop : 要遍历列表,您应该使用for循环

for (var i = 0; i < library.length; i++) {
  var obj = library[i];
  ...
}

Or the forEach method : forEach方法

library.forEach(function(obj) {
  ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM