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.
let array = [
{ name: 'John', age: '18', address: '' },
{ name: 'George', age: '', address: '' },
{ name: 'Kevin', age: '25', address: '' }
]
Object.keys(array[0])
.filter(k => array.every(obj => !obj[k]))
.forEach(k => array.forEach(obj => delete obj[k]));
console.log(array);
Related Topics
Where Is the Rails Method That Converts Data from 'Datetime_Select' into a Datetime Object
Passing a Method as a Parameter in Ruby
Using Layouts in Haml Files Independently of Rails
What Are the Magic $-Prefixed Variables in Ruby
How to Access a Variable Defined in a Ruby File I Required in Irb
Ruby on Rails - Unable to Convert "\X89" from Ascii-8Bit to Utf-8 for Xxx/Xxxx/Xxxx
Recommended Development Web Server for Ruby on Rails 3
Rails 2 to Rails 3:Using Link_To Instead of Link_To_Remote (Including Remote and Update)
How to Find a Min/Max with Ruby
Ruby Method Lookup Path For an Object
How to Do Static Content in Rails
Unzip (Zip, Tar, Tag.Gz) Files With Ruby
How to Match Something With Regex That Is Not Between Two Special Characters
Mavericks, Rbenv, Your Ruby Version Is 2.0.0, But Your Gemfile Specified 2.1.1