functionfilter(array, callback){
newArr =[]for(var i =0; i < array.length; i++){if(callback(array[i], i, array)){
newArr.push(array[i])}}return newArr
}
arr.filter((value, index, array)=> value >2)
forEach
functionforEach(arr, callback){for(var i =0; i < arr.length; i++){callback(arr[i], i, arr)}}
arr.forEach((val, index, array)=> console.log(val, index, array))
map
functionmap(arr, callback){var newArr =[]for(var i =0; i < array.length; i++){
newArr.push(callback(arr[i], i, arr))}return newArr
}
arr.map((value, index, array)=> value *2)
reduce
functionreduce(callback, initialState){const hasInitialState = arguments.length >1let result = initialState
for(let i =0; i <this.length; i++){if(i ===0&&!hasInitialState){
result =this[i]}else{
result =callback(result,this[i], i,this)}}return result
}
arr.reduce((accumulator, nextValue, index, array)=> accumulator + nextValue,
optionalSecondParameter
)
some
functionsome(array, callback){for(var i =0; i < array.length; i++){if(callback(array[i], i, array)){returntrue}}returnfalse}
arr.some((value, index, array)=> value <2)
every
functionevery(array, callback){for(var i =0; i < array.length; i++){if(callback(array[i], i, array)===false){returnfalse}}returntrue}
arr.every((value, index, array)=> value <2)
Function Methods
call
var person ={firstName:"Jason",sayHi:function(){return"Hi "+this.firstName
},determineContext:function(){returnthis=== person
},dog:{sayHi:function(){return"Hi "+this.firstName
},determineContext:function(){returnthis=== person //false},},}
person.dog.sayHi.call(person)
person.dog.determineContext.call(person)//now it is truevar damon ={firstName:"Damon",}
person.sayHi.call(damon)functionsayHello(){return"Hello "+this.firstName
}
console.log(sayHello(damon))//Hello undefined
console.log(sayHello.call(damon))// Hello Damonvar divs = document.getElementByTagName("div")
divs.filter(val=> val)// undefinedvar divsArray =[].slice.call(divs)
divsArray.filter(function(val){return val.innerText ==="Hello"})//now you get a result
apply
functionsayHi(){return"Hi "+this.firstName
}var damon ={firstName:"Damon",}var jason ={firstName:"Jason",}
console.log(sayHi.call(damon))
console.log(sayHi.apply(jason))//seems similar//but with parametersfunctionaddNumbers(a, b, c, d){returnthis.firstName +" just calculated "+(a + b + c + d)}
console.log(addNumbers.call(damon,1,2,3,4))
console.log(addNumbers.apply(jason,[1,2,3,4]))//takes an array as a parametervar nums =[5,7,1,4,2]
console.log(Math.max(nums))//NaN because it doesn't take any arrays
console.log(Math.max.apply(this, nums))functionsumValues(a, b, c){return a + b + c
}var values =[1,2,3]
console.log(sumValues(values))// "1,2,3undefinedundefined"
console.log(sumValues.apply(this, values))// you get the result
bind
//bindfunctionaddNumbers(a, b, c, d){returnthis.firstName +" just calculated "+(a + b + c + d)}var damon ={firstName:"Damon",}//bind returns a new function with 'this' bound alreadyvar damonCalc =addNumbers.bind(damon,1,2,3,4)
console.log(damonCalc())var damonCalc2 =addNumbers.bind(damon,1,2)
console.log(damonCalc2())//NaN
console.log(damonCalc2(3,4))//gives you the result it continues where bind left ofvar jason ={firstName:"Jason",sayHi:function(){setTimeout(function(){//this refers to window because setTimeout is window function
console.log("Hi "+this.firstName)}.bind(this),1000)//this is how we fix it},}
jason.sayHi()
A personal blog by Volkan Uyarer. Developer, Lifelong Learner, Tech Enthusiast.