首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在map操作符es6中执行条件

如何在map操作符es6中执行条件
EN

Stack Overflow用户
提问于 2018-09-07 04:47:04
回答 1查看 40关注 0票数 0

我需要在map函数中检查一个条件,在单个数字值前加上0,这意味着00.00到09.30的剩余值是相同的。

当前,它是所有值的前缀。

代码:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2018-09-07 04:50:14

代码语言:javascript
复制
[...this.times]=x.map(i=>{
    if ( condition ) {
        i = '0'+i;
    }

    return i;
});

谢谢Alexander Staroselsky,我打错了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52211947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档