Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove undefined values from array but keep 0 and null

Tags:

lodash

In javascript, I want to remove undefined values, but keep the values 0 and null from an array.

[ 1, 2, 3, undefined, 0, null ] 

How can I do it cleanly?

like image 680
JLavoie Avatar asked Sep 15 '16 20:09

JLavoie


People also ask

How do you remove an undefined value from an array?

To remove all undefined values from an array:Create an empty array that will store the results. Use the forEach() method to iterate over the array. Check if each element is not equal to undefined . If the condition is met, push the element into the results array.

How do you get rid of undefined?

To remove all undefined values from an object:Use the Object. keys() method to get an array of the object's keys. Use the forEach() method to iterate over the array and delete all undefined values using the delete operator.

How do you remove empty elements from an array?

To remove all empty elements from an array:Call the filter() method, passing it a function. For each element in the array, check if its value is not equal to null and it's not equal to undefined . The filter method returns a new array containing only the elements that satisfy the condition.

How do you remove undefined and null values from an object using Lodash?

To remove a null from an object with lodash, you can use the omitBy() function. If you want to remove both null and undefined , you can use . isNull or non-strict equality.

How to remove only undefined values from an array in JavaScript?

Using lodash, the following remove only undefined values from the array: var array = [ 1, 2, 3, undefined, 0, null ]; _.filter (array, function (a) { return !_.isUndefined (a) } --> [ 1, 2, 3, 0, null ] Or, the following will remove the undefined, 0 and null values: _.filter (array) --> [1, 2, 3]

How to check if an array contains an undefined element?

The function is predicate and checks each element in an array is not equal to undefined. The filter () method will return the new Array, excluding the undefined values The filter method will not modify the contents of the original Array. It returns a new Array containing the elements that have satisfied the condition.

How to remove ‘0’ and empty values from an array in JavaScript?

To remove ‘0’. ‘undefined’ and empty values, you need to use the concept of splice (). Let’s say the following is our array − To run the above program, you need to use the following command − node fileName.js. Here, my file name is demo88.js.

How to remove all holes from an array in JavaScript?

To remove all holes from an array, the idea is to call the filter () method with a callback function that returns true. Note that this approach only remove holes from the array but doesn’t remove any of the other falsy values. 4. Remove all falsy values


2 Answers

You can use _.compact(array);

Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.

See: https://lodash.com/docs/4.15.0#compact

like image 113
Jaynam Avatar answered Sep 21 '22 14:09

Jaynam


The best way using lodash is _.without

Example:

const newArray = _.without([1,2,3,undefined,0,null], undefined); 
like image 30
Piotr Białek Avatar answered Sep 18 '22 14:09

Piotr Białek