I have an array that has a lot of values including a lot of ‘false’ values.
Is there any way of removing the ‘false’ values that is faster than this:
function removeFalseValues (arr:Array):Array {
var ret:Array = new Array ();
for (var i:int = 0; i < arr.length; i ++) {
if (arr[i]) ret.push(arr[i]);
}
return ret;
}
?
You can use the method
filter.For example:
Note that this example, like the code you posted will also filter other items such as
0,nullobjects and empty strings.If you want to keep these values, you can change the previous code into: