如何在地图运算符es6中执行条件

时间:2018-09-06 20:47:04

标签: angular ecmascript-6 rxjs angular6

我需要检查map函数中的条件,以在零之前添加一位数字值,这意味着00.00到09.30的剩余值是相同的。

当前,所有值都位于该值的前面。

代码:

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);
;}

}

演示:enter link description here

1 个答案:

答案 0 :(得分:2)

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

    return i;
});

谢谢Alexander Staroselsky,我打错了文字。