I've the following implementation.
Array.prototype.abc = function(condition, t){
var arr = [];
for( var i = 0; i < this.length; i++){
arr.push(condition(this[i],t));
}
return arr;
};
var a = [1,2,3,4];
var t = 2;
alert(a.abc( function(item,diviser){
if(item % diviser === 0) { return item; }
},t));
The result should be [2,4]. Instead I'm getting [,2,,4].
I've tried the following conditions but it always returns the above result.
if(condition(this[i],t) !== false){ arr.push(condition(this[i],t)); }
This I'm doing when I do
if(item % diviser === 0) { return item; } else { return false; }
The false condition doesnot work for 'false' or true or 'true' or for that matter any value I return in else part. In the actual implementation I do not have the else part though. I know we can use splice or something to remove the blank sections. But I do not understand even if i don't return anything why blank in being created. And even after using if condition before arr.push it does not work. What exactly is happening and how to remove the blanks in the array?