# JavaScript 过滤 NaN

let data = [NaN, 'NaN',1,21,32,NaN,41,5];

// 通过 isNaN 方法判断
let filters1 = function(array) {
  return array.filter(item => {
    return typeof item !== 'number' || !isNaN(item);
  });
};

// 通过转换字符串判断
let filters2 = function(array) {
  let result = [];
  array.forEach(item => {
    if (typeof item !== 'number' ||  item + '' !== 'NaN') {
      result.push(item);
    }
  });
  return result;
};

// 通过 Map 对象判断
let filters3 = function(array) {
  let type = new Map([
    [NaN, 'NaN']
  ]);
  let result = [];
  array.forEach(item => {
    if (!type.get(item)) {
      result.push(item);
    }
  });
  return result;
};

// 通过 NaN 不等于 NaN 判断
let filters4 = function(array) {
  let typeNaN = function(val) {
    return val !== val;
  };
  let result = [];
  array.forEach(item => {
    if (!typeNaN(item)) {
      result.push(item);
    }
  });
  return result;
};

console.log(
  filters1(data)
);
console.log(
  filters2(data)
);
console.log(
  filters3(data)
);
console.log(
  filters4(data)
);