Blake Rivell Blake Rivell - 9 months ago 49
Javascript Question

Filtering out null values of object list in JavaScript

I am currently filtering out null values from an array using Object.keys and map like this:

// The ultimate goal is to get the length of my list where !== null
var length = Object.keys(myList).map(x => myList[x]).filter(x => x !== null).length;

I need to find an alternative way of doing this because in IE11 I am having issues with it. It is interfering with functionality of a 3rd party control somehow.

Any ideas?


The Arrow functions are not supported in IE. So , the equivalent of your code is:

var myList = {'1': '1', '2': '2', '3': '3', '4': null};
var length =   Object.keys(myList).map(function (x) {
  return myList[x]
}).filter(function (x) {
  return x !== null;


Because the output of Object.keys(myList) is an array and you need only to filter elements by their vaues (not null) you may reduce all to:

var myList = {'1': '11', '2': '22', '3': '33', '4': null};
var length =   Object.keys(myList).filter(function (x) {
  return myList[x] !== null;