how to create an array of times with half hour intervals instead of declare in variable

后端 未结 3 436
深忆病人
深忆病人 2021-01-24 04:42

i have an long const values below like this ,is it possible to create in function in typescript to reduce a long const.

export const DepartTime = 
[\'00.00\', \'         


        
相关标签:
3条回答
  • 2021-01-24 04:49

    I don't think you can reduce it. This may not help, but here is JS version :

    var foo = [];
    for (var i = 0; i <= 48; i++) {
       var n = i%2==0 ? i/2+'.00' : (i+1)/2-1+'.30';
        if(n<10) //zero-left padding
       n = '0'+n;
       foo.push(n);
    }
    console.log(foo);
    
    0 讨论(0)
  • 2021-01-24 05:01

    Below code works for me I have tried using https://stackoverflow.com/a/8043061/9516784

        var hrs = [];
        for (var i = 0; i <= 48; i++) {
          var n = i%2==0 ? i/2+'.00' : (i+1)/2-1+'.30';
          if(n<10) {
            n = '0'+n;
          }   
          hrs.push(n);
        }
    
    0 讨论(0)
  • 2021-01-24 05:04

    You can just use division by 2.

    let DepartTime = [...Array(48)].map((e, i) => {
        return (i/2<10 ? '0' : '') + (i/2 - i/2 % 1) + (i/2 % 1 != 0 ? '.30' : '.00');
    });
    console.log(DepartTime);

    0 讨论(0)
提交回复
热议问题