While working on this example on the MDN site explaining the filter method:
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);
This is output in FireBug, as expected:
Filtered array [Object { id=15}, Object { id=-1}, Object { id=0}, Object { id=3}, Object { id=12.2}] Number of Invalid Entries: 4
But I initially mistakenly, but intentionally typed the first console.log (); as:
console.log('Filtered array\n' + arrById);
and got this FireBug output:
Filtered array [object Object],[object Object],[object Object],[object Object],[object Object] Number of Invalid Entries: 4
Why different outputs?
source share