How to Remove Blank Elements from an Array

Remove empty array elements

As you're dealing with an array of strings, you can simply use array_filter(), which conveniently handles all this for you:

print_r(array_filter($linksArray));

Keep in mind that if no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact string '0', you will need a custom callback:

// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));

Note: If you need to reindex the array after removing the empty elements, use: $linksArray = array_values(array_filter($linksArray));

Remove empty elements from an array in Javascript

EDIT: This question was answered almost nine years ago when there were not many useful built-in methods in the Array.prototype.

Now, certainly, I would recommend you to use the filter method.

Take in mind that this method will return you a new array with the elements that pass the criteria of the callback function you provide to it.

For example, if you want to remove null or undefined values:

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];
var filtered = array.filter(function (el) { return el != null;});
console.log(filtered);

How do I remove blank elements from an array?

There are many ways to do this, one is reject

noEmptyCities = cities.reject { |c| c.empty? }

You can also use reject!, which will modify cities in place. It will either return cities as its return value if it rejected something, or nil if no rejections are made. That can be a gotcha if you're not careful (thanks to ninja08 for pointing this out in the comments).

How to remove empty array values ( ) from an array?

You could use the filter like:

arr = arr.filter(item => item);

Example:

let arr = ['One', 'Two', '', 'Four', '', ''];
arr = arr.filter(item => item);
console.log(arr);

// Result
// ['One', 'Two', 'Four']

Because an empty string evaluates to boolean false.
It works with all falsy values like 0, false, null, undefined, '', etc.

DEMO

If you want to keep some values like number 0 (zero) you could use item !== undefined. This filters only undefined values. Keep in mind to trim your string or check with regex to ensure empty strings without whitespaces.

How to remove blank elements from an array of strings in C?

You should use an intermediate one-dimensional character array in the call of fgets like for example

for ( char line[LINE_SIZE]; fgets( line, LINE_SIZE, fp) != NULL; )
{
if ( line[0] != '\n' )
{
line[ strcspn( line, "\n" ) ] = '\0';
strcpy( str[i++], line );
}
}

If a line can contain blanks you can change the condition of the if statement the following way

for ( char line[LINE_SIZE]; fgets( line, LINE_SIZE, fp) != NULL; )
{
size_t n = strspn( line, " \t" );

if ( line[n] != '\n' && line[n] != '\0' )
{
line[ n + strcspn( line + n, "\n" ) ] = '\0';
strcpy( str[i++], line );
}
}

In the above code snippet you can substitute this statement

strcpy( str[i++], line );

for this statement if you want that the string would not contain leading spaces.

strcpy( str[i++], line + n );

How to remove empty elements from nested arrays

You Need return the filter on x, or shorten it. return a value from your map() callback, either by using the return statement or removing the braces to make it an expression arrow function.

const filteredAutor = parsedAutor.map((x) => x.filter((y) => y !== ''));

or

const filteredAutor = parsedAutor.map((x) => {
return x.filter((y) => y !== '');
});

Remove empty values from array of objects only if the value is empty in all objects

If you don't mind mutating the original array object. Here's a solution utilizing some array functions.