skip to Main Content

Write a JavaScript function to remove. null, 0, "", false, undefined and NaN values from an array.

Sample array

[NaN, 0, 15, false, -22, '',undefined, 47, null]

Expected result

[15, -22, 47] 

I am getting the result as : [ NaN ]
Is there any other way to perform this action?*

const arr = [0, NaN, 15, false, -22, '', undefined, 47, null]
let result = []

for (let i = 0; i <= arr.length; i++) {

  if (arr[i] !== 0 && arr[i] !== false && arr[i] !== '' && arr[i] !== undefined && arr[i] !== null && !isNaN(arr[i]) === false) {

    result.push(arr[i])
  }
}

console.log(result)

2

Answers


  1. Looks like you want to keep truthy values. You can pass filter and just use the value itself as the predicate using the identity function (x => x).

    const arr = [0, NaN, 15, false, -22, '', undefined, 47, null]
    const result = arr.filter(a => a)
    
    console.log(result)

    If filter doesn’t work for you, it’s the equivalent of looping over the input, and if a condition is true, adding it to the output one.

    const arr = [0, NaN, 15, false, -22, '', undefined, 47, null]
    const result = []
    
    for (i in arr) {
      const item = arr[i];
      if (item) {
        result.push(item);
      }
    }
    
    console.log(result)

    And here is the same thing by tracking the index yourself as in the original snippet:

    const arr = [0, NaN, 15, false, -22, '', undefined, 47, null]
    const result = []
    
    for (let i = 0; i < arr.length; i++) {
      const item = arr[i];
      if (item) {
        result.push(item);
      }
    }
    
    console.log(result)

    Notice how the main point is that we are exploiting the concept of truthiness in JavaScript: every value at runtime can be evaluated as either true or false, regardless of whether they are a boolean or not. 0, NaN, false, undefined, and null are all evaluated to false when a boolean is required (like when expressing a condition for an if statement), while non-zero numbers are all evaluated to true.

    For more on the concept and how JavaScript decides whether a value is truthy or not, you can read more here: https://developer.mozilla.org/en-US/docs/Glossary/Truthy

    Login or Signup to reply.
  2. you can filter the array to include only the numbers. The main issue in your code is the condition !isNaN(arr[i]) === false, which is not working as expected. Instead, you can use the typeof operator to check if the value is a number and isFinite to ensure it is not NaN.

    const arr = [NaN, 0, 15, false, -22, '', undefined, 47, null];
    let result = [];
    
    for (let i = 0; i < arr.length; i++) {
      if (typeof arr[i] === 'number' && arr[i] !== 0 && isFinite(arr[i])) {
        result.push(arr[i]);
      }
    }
    
    console.log(result); // [15, -22, 47]

    Alternatively, you can use the filter method,

    const arr = [NaN, 0, 15, false, -22, '', undefined, 47, null];
    
    const result = arr.filter(item => typeof item === 'number' && item !== 0 && isFinite(item));
    
    console.log(result); // [15, -22, 47]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search