JS按时间距离分割数组问题、数组切片求指教?

默认数组:

let defData = [
    {
        time: "09:00:00"
    },
    {
        time: "09:01:20"
    },
    {
        time: "09:08:00"
    },
    {
        time: "09:10:00"
    },
    {
        time: "09:11:35"
    },
    {
        time: "09:15:07"
    },
    {
        time: "09:18:00"
    },
    {
        time: "13:00:01"
    },
    {
        time: "13:00:01"
    },
    {
        time: "13:00:05"
    },
    {
        time: "13:01:32"
    },
    {
        time: "13:02:01"
    },
    {
        time: "13:05:01"
    }
];

需求描述:

我的需求是按时间重新分割数组,组成一个新的2维数组,每个成员属性包含10分钟数据,这样的结构:

let data = [
    [
        {time: "09:00:00"},
        // ...省略中间
        {time: "09:10:00"}
    ],
    [
        {time: "09:11:00"},
        // ...省略中间
        {time: "09:20:00"}
    ]
];

我自己写了个函数做时间对比的方式,但结果时间跳跃跨度太大但时候就抽风了,求大神帮忙给个小demo,在线急等···!拜托啦,这个问题卡了我快1天了。

阅读 2.5k
1 个回答
   let defData = [{
                time: "09:00:00"
            },
            {
                time: "09:01:20"
            },
            {
                time: "09:08:00"
            },
            {
                time: "09:10:00"
            },
            {
                time: "09:11:35"
            },
            {
                time: "09:15:07"
            },
            {
                time: "09:18:00"
            },
            {
                time: "13:00:01"
            },
            {
                time: "13:00:01"
            },
            {
                time: "13:00:05"
            },
            {
                time: "13:01:32"
            },
            {
                time: "13:02:01"
            },
            {
                time: "13:05:01"
            }
        ];
        function repeatTimes(arr){
            var newArr = [];
            if(Object.prototype.toString.call(arr).slice(8,-1).toLowerCase().indexOf('array') === -1)return;
            arr.map(function(t){
                var new_time = t.time,
                    f_hour = new_time.slice(0,new_time.indexOf(':') + 1),
                    s_minute = String(Number(new_time.slice(new_time.indexOf(':') + 1,new_time.lastIndexOf(':'))) + 10),
                    t_second = new_time.slice(new_time.lastIndexOf(':'));
                newArr.push([
                    t,
                    {
                        time:f_hour + s_minute + t_second
                    }
                ])
            })
            return newArr;
        }
        //调用
        repeatTimes(defData);
        
        

是这个意思?等等我修改一下。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏