3

I tried using isNaN(value) and !isNaN(value) but I am unable to remove the NaN element in the give code without removing the strings. (Obviously because string is not a number).

function cleaner(arr) {
  return = arr.filter(function f(value) {return (value !== false && value !== null && value !== 0 && value !== undefined)});
}
cleaner([7, "eight", false, null, 0, undefined, NaN, 9, ""]);

The above code should return [7, "eight", 9, ""];

8
  • P.s, I think I could have worded the title better but I'm not sure how. Commented Jan 28, 2016 at 3:16
  • false and null and undefined are not NaNs. Only NaN is NaN Commented Jan 28, 2016 at 3:16
  • 1
    Regardless, the last line of my post will explain what I actually want, to quell any confusion. Commented Jan 28, 2016 at 3:19
  • 4
    Guys, seriously, the question has nothing to do with NaNs. Commented Jan 28, 2016 at 3:19
  • 2
    Just check if typeof of the value is either a string or a number and you're done. Commented Jan 28, 2016 at 3:21

3 Answers 3

5

This will return only numbers (without 0) and strings (including empty ones).

function cleaner(arr) {
   return arr.filter(function(item){ 
      return typeof item == "string" || (typeof item == "number" && item);
              /** Any string**/        /** Numbers without NaN & 0 **/
   });
}
console.log(cleaner([7, "eight", false, null, 0, undefined, NaN, 9, ""]));
//[7, "eight", 9, ""]

Using ES2015 Arrow Function syntax

array.filter(item => typeof item == "string" || (typeof item == "number" && item));

var filteredArr = [7, "eight", false, null, 0, undefined, NaN, 9, ""].filter(item => typeof item == "string" || (typeof item == "number" && !isNaN(item) && item));

console.log(filteredArr);

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

3 Comments

close, but it still does not remove the 0.
@salmanxk now without 0, I thought you said numbers and string, but I removed 0 now.
Well, in case it matters, this will nuke all functions, objects, arrays. Is that what you want, or you don't care?
3

Some ways to detect whether x is NaN:

  • Number.isNaN(x) (ES6)
  • Object.is(x, NaN) (ES6)
  • x !== x
  • (typeof x === 'number') && isNaN(x)

Note: isNaN alone is not a reliable way to check if a value is NaN!

4 Comments

Oh gosh, you answered the title without checking the question body?
@zerkms No, I read it. OP is using filter to remove some items from an array. He wants to remove NaNs too, but using !isNaN removed both NaN and strings. That's because isNaN is not a reliable way to check if a value is NaN. Or that's what I understood, at least.
@zerkms It's you who didn't read the question! XD "I tried using isNaN(value) and !isNaN(value) but I am unable to remove the NaN element in the give code without removing the strings".
Lol, I've got caught
0

Since you want to filter out all falsy values except empty strings, all you need is

function cleaner(arr) {
  return arr.filter(function(value) {
    return value === '' || value;
  });
}

If you had wanted to filter out all falsy values including empty strings, you could have done:

function cleaner(arr) {
  return arr.filter(Boolean);
}

5 Comments

Btw, that's not what OP wanted.
running your code(filter(function(value) { return isNaN(value); }) ) on the array [7, "eight", NaN,] returns ["eight", null]. I need it to return [7,"eight].
There is a reason I did not use .filter(Boolean); Boolean returns false on all 0, -0, null, false, NaN, undefined, and the empty string (""). I need it to return on some of these but not all.
No, actually you want to return false on all but one (""). That's why it makes sense to simply list that single exception, as I have done in the answer above. The accepted answer has the unfortunate behavior that it removes all objects, arrays, functions, regexps, etc. etc., which may or may be what you want, but seems like a bug waiting to happen. Also it's long and ugly.
Hey @MarcosCasagrande, please re-read the question. Him saying he "wanted numbers and strings" is a pure fantasy made up by you. The sample code he gave would in fact preserve functions etc., so it's a perfectly reasonable assumption that he would want to, and a purely arbitrary assumption by you that he would not.

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.