if we want to use the means we literlly do to the iterations manually
const fruits = ['banana', 'mango', 'jack','graph'];
console.log(fruits)
[ 'banana', 'mango', 'jack', 'graph' ]
for(let i = 0; i < fruits.length; i++){
console.log(fruits[i]);
}
banana
mango
jack
graph
it looks easy if suppose if you i a complex position like this
const fruits = ['banana', 'mango', 'jack','graph'];
function callBackFunction( parameter ){
console.log(value)
}
function callingEachElement( callBackFunction ){
for(let i = 0; i < fruits.length; i++){
callBackFunction(fruits[i]);
}
}
callingEachElement( callBackFunction )
here we using an external function and calling the callback function
instead you writing the whole thing in a loop , how it would be easy if we do
in a one line, yes javascript have many functions do it, on those forEach is one of the
function, let we see forEach
forEach does not return any thing
fruits.forEach( (e)=> console.log(e) );
banana
mango
jack
graph
similary javascript have several functions, let we see them one by one
forEach does return anything but map will do the same thing and return
you an modify array
const fruits = ['banana', 'mango', 'jack','graph'];
function callBackFunction( value ){
return value.toUpperCase();
}
function callingEachElement( callBackFunction ){
const returning = [];
for(let i = 0; i < fruits.length; i++){
returning.push( callBackFunction(fruits[i]) );
}
return returning
}
result = callingEachElement( callBackFunction )
console.log(result)
[ 'BANANA', 'MANGO', 'JACK', 'GRAPH' ]
const ret = fruits.map( (e) => e.toUpperCase() );
console.log(ret);
[ 'BANANA', 'MANGO', 'JACK', 'GRAPH' ]
flatMap function
const intgers = [1,2,3,4,5,6,7,8,9,10];
function callBackFunction( value ){
return [value, value*value];
}
function findSquare( callBackFunction ){
const returning = [];
for(let i = 0; i < intgers.length; i++){
returning.push( callBackFunction(intgers[i]) );
}
return returning.flat();
}
result = findSquare( callBackFunction )
console.log(result)
[
1, 1, 2, 4, 3, 9, 4,
16, 5, 25, 6, 36, 7, 49,
8, 64, 9, 81, 10, 100
]
const ret = intgers.flatMap( (e) => [e, e*e] );
console.log(ret);
[
1, 1, 2, 4, 3, 9, 4,
16, 5, 25, 6, 36, 7, 49,
8, 64, 9, 81, 10, 100
]
filter function
const intgers = [10,5,7,3,2,8,4,1,6,9];
function callBackFunction( value ){
if (value <= 5){
return value
}
else{
return false
}
}
function findSquare( callBackFunction ){
const returning = [];
for(let i = 0; i < intgers.length; i++){
res = callBackFunction(intgers[i]);
if (res != false){
returning.push( res );
}
}
return returning
}
result = findSquare( callBackFunction )
console.log(result)
const ret = intgers.filter( (value) => {
if (value <= 5){
return value
}
} );
console.log(ret);
reduce function
const intgers = [10,5,7,3,2,8,4,1,6,9];
function callBackFunction( total, value ){
console.log(total, value)
return total + value
}
function findSum( callBackFunction ){
let returning = 0;
for(let i = 0; i < intgers.length; i++){
res = callBackFunction(returning,intgers[i]);
returning += res
}
return returning
}
result = findSum( callBackFunction )
console.log(result)
const ret = intgers.reduce( (total , values) => total);
console.log(ret);
Top comments (0)