Map Reduce Functions
a=1..10; var result=[]; for(var i=0;i<a.length;i++) { console.log(a[i],i) result.push((a[i])^2)
} result
Using map function a=1..10; a.map( function (x,i) { console.log(x,i) return(x^2) } )
a.map((x,i)=>x^2)
a.$("x^2")
f:=x^2; a.$(f)
Reduce function a=1..10; a.reduce((x,i,last)=>x+last,10)
a.reduce(
function(last,x,i)
{
return(x+last)
},
10
)
a.reduce((last,x,i)=>x+last,10)
a
var sum=0; for(var i=0;i<a.length;i++) { sum=sum+a[i]
} sum
a.reduce( function(last,x, i) { return(x+last) }, 10 )
a.reduce( function(last,x, i) { return(Math.max(x,last)) }, 60 )
a.reduce( function(last,x, i) { return(Math.min(x,last)) }, 60 )
a.reduce(
function(last,x,i)
{
console.log(last,x,i)
return(x+last)
},
0
)
a.reduce( function(last,x,i) { console.log(last,x,i) return(last-x) }, 0 )
a.reduce(
function(last,x,i)
{
console.log(last,x,i)
return(last*x)
},
1
)
SUM(a)
PRODUCT(a)
1/2/3/4/5/6/7/8/9/10
a.reduce( function(last,x,i) { console.log(last,x,i) return(last/x) }, 1 )
a.filter(
function(x,i)
{
return(x%2)
}
)
a.filter(
function(x,i)
{
return(x%2==1)
}
)
var result=[];
for(var i=0;i<a.length;i++)
{
if(a[i]%2==1)
{
result.push(a[i])
}
} result
a.filter( function(x) { return(x%2==1) } )
a.filter(x=>x%2==1)
var result=[]; for(var i=0;i<a.length;i++) { if(i%4==0) { result.push(a[i]) }
} result
a=1..100; var result=[]; for(var i=0;i<a.length;i++) { if(i%4==0) { result.push(a[i]) }
} result
a=1..100;
var result=[];
for(var i=0;i<a.length;i=i+4)
{
result.push(a[i])
}
result
a=1..100;
var result=[];
for(var i=0;i<a.length;i++)
{
if(((i%4)==3)||(i%4)==0)
{
result.push(a[i])
}
} result
a=1..100;
var result=[];
for(var i=0;i<a.length;i++)
{
if(((i%4)==3)||((i%4)==0))
{
result.push(a[i])
}
} result
a=1..100; var result=[]; for(var i=0;i<a.length;i++) { if((i%4)==0) { result.push(a[i-1]) }
} result
a=1..100;
var result=[];
for(var i=0;i<a.length;i=i+4)
{
result.push(a[i-1])
result.push(a[i])
}
result
a=1..100;
var result=[];
for(var i=2;i<a.length;i++)
{
if((i%3)==0)
{
result.push(a[i])
}
if((i%4)==0)
{
result.push(a[i])
}
} result
a=1..100; var result=[]; for(var i=2;i<a.length;i++) { if((i%3)==2) { result.push(a[i]) } if((i%4)==3) { result.push(a[i]) }
} result
vehicles= [ { speed: 23, color:"red", price:34.56 }, { speed: 22, color:"orange", price:14.56 }, { speed: 12, color:"green", price:34.56 }, { speed: 1.2, color:"grey", price:134.56 }, { speed: 62, color:"white", price:54.56 } ]
vehicles
.map(v=>v.speed+2)
vehicles.f(v=>v.color=="orange" || v.speed>50)
vehicles
.f(v=>v.speed>50)
.reduce(
function(last,v,i)
{
return(last+v.price)
},
0
)
vehicles
.filter(v=>v.speed>50)
.reduce(
function(last,v,i)
{
return(last+v.price)
},
0
)
vehicles
.filter(v=>v.speed>20)
.reduce(
function(last,v,i)
{
return(last+v.price)
},
0
)
vehicles .filter(v=>v.speed>20) .map(v=>v.price*20) vehicles .filter(v=>v.speed>20) .map(v=>v.price*20%)
vehicles .filter( function(v) { return( v.color=="orange" || v.speed>50
) } ) /*var result=[] for(var i=0;i<vehicles.length;i++) { if(vehicles[i].color=="orange") { result.push(vehicles[i]) } } console.log(result)*/
var result=[] for(var i=0;i<vehicles.length;i++) { if(vehicles[i].color=="orange") { result.push(vehicles[i]) } } console.log(result) vehicles .map(v=>v.speed=v.speed+3)
vehicles .map(v=>v.speed)
vehicles.f(v=>v.color=="orange" || v.speed>50)
vehicles
.filter(v=>v.speed>20)
.map(v=>v.price*120%)
vehicles .filter(v=>v.speed>20) .map(v=>[v.color,v.price*120%])
vehicles .filter(v=>v.speed>20) .map(v=>[v.speed,v.color,v.price*120%])
vehicles .filter(v=>v.speed>20) .map(v=>[v.speed,v.color,v.price*80%])
newvehicles= [ { speed: 23, color:"red", price:34.56, power:243 }, { speed: 22, color:"orange", price:14.56, power:233 }, { speed: 12, color:"green", price:34.56, power:53 }, { speed: 1.2, color:"grey", price:134.56 }, { speed: 62, color:"white", price:54.56 } ]
newvehicles.filter(v=>!v.power)
newvehicles.filter(v=>v.power)
newvehicles.filter(v=>!v.power)
newvehicles .filter(v=>!v.power) .map(v=>[v.speed,v.color,v.price*80%]) speed color price power 23 red 34.56 243 22 orange 14.56 233 12 green 34.56 53 1.2 grey 134.56 62 white 54.56