0

I need to check a condition in map function, to prepend zeros for single digit values which means 00.00 to 09.30 remaining values be same.

currently it is prepending for all the values.

code:

export class SelectOverviewExample implements OnInit {
  days=[];
  times =[];



ngOnInit(){
  [...this.days] = weekdays;
  // [...this.times]=IntervalTime;
  this.OnCall();
}

OnCall(){
var toInt  = time => ((h,m) => h*2 + m/30)(...time.split(':').map(parseFloat)),
    toTime = int => [Math.floor(int/2), int%2 ? '30' : '00'].join(':'),
    range  = (from, to) => Array(to-from+1).fill().map((_,i) => from + i),
    eachHalfHour = (t1, t2) => range(...[t1, t2].map(toInt)).map(toTime);
let x=[];

[...x]= eachHalfHour('0:00','15:30');

[...this.times]=x.map(i=>'0'+i);
;}

}

demo:enter link description here

Mohamed Sahir
  • 2,482
  • 8
  • 40
  • 71
  • You should not do it in a separate `map`. You should do it to `Math.floor(int/2)` in `toTime`. – Bergi Sep 06 '18 at 21:01

1 Answers1

1
[...this.times]=x.map(i=>{
    if ( condition ) {
        i = '0'+i;
    }

    return i;
});

Thanks Alexander Staroselsky, I made a typo.

Karl Johan Vallner
  • 3,980
  • 4
  • 35
  • 46
  • 1
    With the braces `{}` you need a `return` statement to actually map the value `i = '0' + i;`. Without a return statement it will be type `void[]` and will come out as an empty array. – Alexander Staroselsky Sep 06 '18 at 21:03