You can apply the filter method on the array by passing Boolean as a parameter. This way it removes all falsy values(0, undefined, null, false and “”) from the array.
1 2 3 4 | const myArray = [false, null, 1,5, undefined] // is same as myArray.filter(x => x); myArray.filter(Boolean); // [1, 5] |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.