0

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?

1
  • I think the better approach would be to see how this can affect the other library. Commented Sep 7, 2016 at 13:57

2 Answers 2

1

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;
}).length;

console.log(length);

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;
}).length;

console.log(length);

Sign up to request clarification or add additional context in comments.

Comments

1
var length = 0

for (var i = 0; i < myList.length; i++) {
  if (myList[i] !== null) { 
     length++; 
  } 
}

In this case, the for-loop is your map and the if-condition is your filter.

1 Comment

This is exactly what I needed to do. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.