How to remove null elements from array in JavaScript

Every now and then arrays normally tend to have null or otherwise empty elements

Array filter is a specialized JavaScript function for filtering array elements found to be matching a given criteria for example;

const arr = ['max', 'duke']

In order to remove duke from elements of the array you can do it as follows;

arr.filter(name => (name == 'duke' ? false : true))

// returns ['max']

You can filter empty and false elements as well, using a similar syntax as done above;

const arr = ['max', null, 'duke']

arr.filter(name => name)
// ['max', 'duke']

Comments

Popular posts from this blog

FlappingBird Postmortem (JS13K)

JavaScript introduction to variables

FlappingBird JS13K Games

How to target all elements of a given class name in JavaScript

Zedplug will be decommissioned on November 30th