Array.prototype.filter()_Having difficulty understanding the output

Array.prototype.filter()_Having difficulty understanding the output
0

#1

I am reading the following page on the filter method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter and came across an example (copied in further below) which illustrates how to filter invalid entries from JSON.

My question:
function filterByID(item) returns true and false, so why does console.log('Filtered Array\n', arrByID); return [{ id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }] instead of true and false? Thank you in advance.

var arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  { },
  { id: null },
  { id: NaN },
  { id: 'undefined' }
];

var invalidEntries = 0;

function isNumber(obj) {
  return obj!== undefined && typeof(obj) === 'number' && !isNaN(obj);
}

function filterByID(item) {
  if (isNumber(item.id)) {
    return true;
  } 
  invalidEntries++;
  return false; 
}

var arrByID = arr.filter(filterByID);

console.log('Filtered Array\n', arrByID); 
// Filtered Array
// [{ id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }]

console.log('Number of Invalid Entries = ', invalidEntries); 
// Number of Invalid Entries = 4

#2

The filter function returns a “filtered” array using the filterByID function. The filter function takes a function as an argument (in this case filterByID).

Each item of the array arr is passed to the filterByID function. Only items in which filterByID returns true are assigned to the new array called arrByID.


#3

Look at another example of how to use filter. Let’s say I have an array of the numbers 1 through 10 and want to create another array with only the numbers greater than 7, then I could use the following code to do that:

var arr = [1,2,3,4,5,6,7,8,9,10];

function filterNumbersGreaterThanSeven(item) {
  if (item > 7) {
    return true;
  } 
  return false; 
}

var arrNumsGreaterThanSeven = arr.filter(filterNumbersGreaterThanSeven);

console.log('Filtered Array\n', arrNumsGreaterThanSeven); 
// Filtered Array
// [8,9,10]