Javascript Array filter() filter object array

Description

Javascript Array filter() filter object array

let arr = [//from  w  w  w  .ja v a  2 s .c  om
  { id: 3 },
  { id: 1.2 },
  { },
  { id: null },
  { id: NaN },
  { id: 'undefined' }
]

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

function filterByID(item) {
  if (isNumber(item.id) && item.id !== 0) {
    return true
  } 
  return false;
}

let arrByID = arr.filter(filterByID)
console.log('Filtered Array\n', arrByID)



PreviousNext

Related