working link==>https://stackblitz.com/edit/js-wpw67a
//check out filter map and reduce once
//create your own map function or polyfill for map
//map callback accept three parameter one value second index and third one is a current array
//create a polyfill for map function
Array.prototype.myMap=function(callBack)
{
if(typeof callBack != "function")
{
throw new TypeError();
}
var newArray=[];
for(var i=0;i<this.length;i++)
{
//this is current Array
//i is index
//this[i] is current item
newArray.push(callBack.apply(null,[this[i],i,this]));
}
return newArray;
}
var myArray=[1,2,3,4,5];
var newArray=myArray.myMap((value,index)=>{
return value*index;
})
console.log(newArray);
//now polyfill for filter which accept three parameter one value second index and third one is a current array
Array.prototype.myFilter=function(callBack)
{
if(typeof callBack != "function")
{
throw new TypeError();
}
var newArray=[];
for(var i=0;i<this.length;i++)
{
if(callBack.apply(null,[this[i],i,this]))
{
newArray.push(this[i]);
}
}
return newArray;
}
//example first-----------------------------
var myArray1=[1,2,3,4,5];
var newArray1=myArray1.myFilter((value,index)=>{
return value>2;
})
console.log(newArray1);
//let other example
var strs=["manish","deep","seema","ok","opps"];
//return only names which contain 5 or more letter
var newNames=strs.myFilter((value)=>{
return value.length>=5;
})
Comments
Post a Comment