Home > Enterprise >  I only need the 0 value in this case from an array of [0, undefined, null, false]
I only need the 0 value in this case from an array of [0, undefined, null, false]

Time:12-15

I have tried the filter and find method to get the value 0 but it did not worked it just returns undefined

[0, undefined, null, false].filter(e => e) // output empty array
[0, undefined, null, false].find(e => e)  // output undefined

If anybody helps me out to find the solution for this I would greatfull for you help

CodePudding user response:

const filtered = [0, undefined, null, false].filter(e => e === 0);
const found = [0, undefined, null, false].find(e => e === 0);

console.log(filtered);
console.log(found);

CodePudding user response:

    var array = [0, undefined, null, false];
var onlyZeros = array.filter(function(value) {
  return value === 0;
});

This code would create a new array called onlyZeros that only contains the 0 value from the original array.

  • Related