JenMorgan / js-learning

0 stars 0 forks source link

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

Open kartamyshev opened 4 years ago

kartamyshev commented 4 years ago

Sample array : [NaN, 0, 15, false, -22, '',undefined, 47, null] Expected result : [15, -22, 47]

JenMorgan commented 4 years ago
function removeFalsy (arr) {
    const cleanArray = [];
    arr.forEach((item) => {
        if (item) cleanArray.push(item)
    })
    return cleanArray;
}
JenMorgan commented 4 years ago
function removeFalsy (arr) {
    return arr.filter(item => item);
}
JenMorgan commented 4 years ago
function removeFalsy (arr) {
    return arr.filter(Boolean);
}