0

Hope this is won't take much time, thank you in advance.

so i want to achieve something like this but i don't know how

let arr = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
  let newArray = [];
  for(let i = 0; i < this.length; i++){
    if(callback(param) === ){ // I want only the filtered items to get pushed to the newArray
      newArray.push(callback(this[i]))
    }
  }
  return newArray;
};

var new_arr = arr.myFilter(function(item) {
  return item % 2 === 1;
});


Plvtinum
  • 187
  • 1
  • 5
  • 11
  • 1
    `if(callback(param) === )` -> `if(callback(this[i]) === true)` or just `if(callback(this[i]))`. But you also need to remove it from the `if` body: `newArray.push(callback(this[i]))` -> `newArray.push(this[i])` – VLAZ Apr 22 '21 at 15:39
  • 1
    Why not use `Array.filter(filter_fn)` for this purpose? – Wazeed Apr 22 '21 at 15:43
  • 1
    @Wazeed seems like an excercise to recreate it. – VLAZ Apr 22 '21 at 15:44
  • Thank you so much, but why we just typed `if( callback(param) )` instead of `if( callback(param) ) === true` – Plvtinum Apr 22 '21 at 15:46
  • @Wazeed because im creating my own array method in a coding challenge – Plvtinum Apr 22 '21 at 15:47
  • `if` will use the value you give it as a boolean. More specifically, *truthy* values are treated as if `true` while *falsy* as if `false`. There is rarely a need to explicitly add `if( value === true )` over `if ( value )`. See [Understanding JavaScript Truthy and Falsy](https://stackoverflow.com/q/35642809) – VLAZ Apr 22 '21 at 15:50

2 Answers2

1

Change your conditional to this:

if (callback(this[i])) {
  newArray.push(this[i]);
}

You need to evaluate the current item i.e. this[i] by passing it to the callback to evaluate the condition as true, in order to push the current item to the newArray.

let arr = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
  let newArray = [];
  for (let i = 0; i < this.length; i++) {
    if (callback(this[i])) {
      newArray.push(this[i]);
    }
  }
  return newArray;
};

var newArr = arr.myFilter(function(item) {
  return item % 2 === 1; // is odd
});

console.log(newArr); // [23, 65, 5];

If you are interested, there is a polyfill for Array.prototype.filter() hosted over at MDN.

if (!Array.prototype.myFilter) {
  Array.prototype.myFilter = function(func, thisArg) {
    'use strict';
    if (!((typeof func === 'Function' || typeof func === 'function') && this)) {
      throw new TypeError();
    }

    var len = this.length >>> 0,
      res = new Array(len), // preallocate array
      t = this,
      c = 0,
      i = -1;

    var kValue;
    if (thisArg === undefined) {
      while (++i !== len) {
        // checks to see if the key was set
        if (i in this) {
          kValue = t[i]; // in case t is changed in callback
          if (func(t[i], i, t)) {
            res[c++] = kValue;
          }
        }
      }
    } else {
      while (++i !== len) {
        // checks to see if the key was set
        if (i in this) {
          kValue = t[i];
          if (func.call(thisArg, t[i], i, t)) {
            res[c++] = kValue;
          }
        }
      }
    }

    res.length = c; // shrink down array to proper size
    return res;
  };
}

let arr = [23, 65, 98, 5];

var newArr = arr.myFilter(function(item) {
  return item % 2 === 1; // is odd
});

console.log(newArr); // [23, 65, 5];
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

Can try this

let arr = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
  let filteredArray = [];
  
  for(let i = 0; i < this.length; i++){
    const currentElement = this[i];
  
    if(callback(currentElement)) {
      filteredArray.push(currentElement);
    }
  }
  
  return filteredArray;
};

var new_arr = arr.myFilter(function(item) {
  return item % 2 === 1;
});

console.log(new_arr);
Showrin Barua
  • 1,737
  • 1
  • 11
  • 22