Skip to content Skip to sidebar Skip to footer

Es6 How To Get Source Code For Filter( ) Method

I've tried to reproduce the method filter so far I have got: function filtre(...[T,x,y]){ let tabPersF = []; for (let pers of T) { if (x(...[pers,y])) { tabPersF.push( pe

Solution 1:

So I've tried to reproduce the method filter I got this and I know it's not good.

Multiple issues with this code

  • array2 not initialized
  • array2 not an array and what is puts
  • function keyword as parameter

Make it

functionfilter(array, func) {
  let array2 = [];
  for ( var counter = 0; counter < array.length; counter++) {       
    if (func(array[counter], counter, array )) {
      array2.push(array[counter]);
    }
  }
  return array2;
}

console.log( filter( [1,2,3], function(s){ return s>1 } ) );

console.log( filter( ["132","322","23433"], function(s){ return s.length>3 } ) );

var arr = [ {name: "asdasd", age: 10}, {name: "asdasd2", age: 20} ];

console.log( filter( arr , function(s){ return s.age>19 } ) );

Post a Comment for "Es6 How To Get Source Code For Filter( ) Method"