根据时间戳分组对象

时间:2019-05-04 01:38:58

标签: javascript

我正在用JavaScript来完成这项任务。我有一个带有时间戳的对象列表:

{ "timestamp": 2019-04-24 07:04:32.820, "data": 0.54 } 
{ "timestamp": 2019-04-24 08:06:22.820, "data": 0.34 } 
{ "timestamp": 2019-04-24 08:41:42.720, "data": 0.89 } 
{ "timestamp": 2019-04-24 09:11:32.430, "data": 0.14 } 
{ "timestamp": 2019-04-24 09:21:31.213, "data": 0.63 }
{ "timestamp": 2019-04-24 10:06:11.120, "data": 0.85 }  

我想根据给定的开始时间每小时对对象进行分组。 因此,如果开始时间是2019-04-24 06:44:00,则第一个时段是从6:44-7:44,然后是7:44-8:44、8:44-9:44和9:44- 10:44等等。对于输入,我想象以下输出:

[ { "1": [{ "timestamp": 2019-04-24 07:04:32.820, "data": 0.54 }] }, 
  { "2": [{ "timestamp": 2019-04-24 08:06:22.820, "data": 0.34 }, 
       { "timestamp": 2019-04-24 08:41:42.720, "data": 0.89 }] },
  { "3": [{ "timestamp": 2019-04-24 09:11:32.430, "data": 0.14 }, 
       { "timestamp": 2019-04-24 09:21:31.213, "data": 0.63 }] },
  { "4": [{ "timestamp": 2019-04-24 10:06:11.120, "data": 0.85 }]

我很不知道该如何把握这个想法,

3 个答案:

答案 0 :(得分:1)

这将为您提供一个代表毫秒数的整数(请参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime):

new Date('2019-04-24 07:04:32.820').getTime();

您拥有一个,将时间戳分组非常简单。 您可以执行以下操作(请注意,输出的格式略有不同,但是您可以理解):

let inputs = [
{ "timestamp": '2019-04-24 07:04:32.820', "data": 0.54 } ,
{ "timestamp": '2019-04-24 08:06:22.820', "data": 0.34 } ,
{ "timestamp": '2019-04-24 08:41:42.720', "data": 0.89 } ,
{ "timestamp": '2019-04-24 09:11:32.430', "data": 0.14 } ,
{ "timestamp": '2019-04-24 09:21:31.213', "data": 0.63 } ,
{ "timestamp": '2019-04-24 10:06:11.120', "data": 0.85 }]

const MS_PER_HOURS = 3600 * 1000;
let startTime = new Date(inputs[0].timestamp).getTime();
let outputs = [];
inputs.forEach(input=>{
  let time = new Date(input.timestamp).getTime();
  let hour = Math.floor((time-startTime) / MS_PER_HOURS);
  if(!outputs[hour]) outputs[hour] = [];
  outputs[hour].push(input);
});

console.log(outputs);

答案 1 :(得分:0)

我为您写了以下内容... 我想你可能会喜欢。请让我认识我的朋友...

var AllTimes = [
    new Date("2019-04-24 09:12:32.430"),
    new Date("2019-04-24 09:33:29.213"),

    new Date("2019-04-24 07:04:32.820"),
    new Date("2019-04-24 08:54:42.720"),

    new Date("2019-04-24 08:06:22.820"),
    new Date("2019-04-24 07:038:11.110"),

    new Date("2019-04-24 07:038:11.110")
];



function renderTimes(unorderedTimes)
{

    var NewTimeEntry = class 
    {
        constructor(startTime, endTime)
        {     
            this.startTime = new Date(startTime);
            this.endTime = new Date(endTime);
        }
    }

    function DateValidationFunction(DateTime)
    {
        return typeof DateTime !== undefined && DateTime !== null && typeof DateTime === "object";
    }




    //Taking a deep copy and then sorting...
    var orderedTimes = unorderedTimes.slice().sort();
    var orderedTimesCount = orderedTimes.length;
    var Pairings = [];

    var GroupNumber = 0;
    for(var i = 0; i < (orderedTimesCount); i=i+2)
    {
        var startTime = orderedTimes[i],
            endTime = orderedTimes[i+1],
            test1 = DateValidationFunction(startTime),
            test2 = DateValidationFunction(endTime);

        if(test1 && test2)
        {
            Pairings.push(new NewTimeEntry(startTime, endTime));
        }
    }

    return Pairings;

}

var Answer = {
    Times: renderTimes(AllTimes),
    Count: renderTimes(AllTimes).length
};

//Displays the Answer...
console.log({Answer})

答案 2 :(得分:0)

这是您要寻找的东西吗??? 按小时划分,喜欢发布...

var AllTimes = [
    new Date("2019-04-24 09:12:32.430"),
    new Date("2019-04-24 09:33:29.213"),

    new Date("2019-04-24 07:04:32.820"),
    new Date("2019-04-24 08:54:42.720"),

    new Date("2019-04-24 08:06:22.820"),
    new Date("2019-04-24 07:038:11.110"),

    new Date("2019-04-24 07:038:11.110")
];



function renderTimes(unorderedTimes)
{
    // Class responsible for keeping track of times in partition...
    var timeManager = class 
    {
        constructor()
        {     
            this.timePartition = {};
        }

        addTime(newTime)
        {
            var Hour = newTime.getHours();
            if(this.timePartition[`Hour_${Hour}`] !== undefined)
            {
                //This Object is defined...
                this.timePartition[`Hour_${Hour}`].push(newTime);

            } else {
                //This Object is NOT defined...
                this.timePartition[`Hour_${Hour}`] = [newTime];
            }
        }
    }


    //Taking a deep copy and then sorting...
    var orderedTimes = unorderedTimes.slice().sort();
    var orderedTimesCount = orderedTimes.length;
    var _timeManager = new timeManager();

    for(var i = 0; i < orderedTimesCount; i++)
    {
        var timeInstance = orderedTimes[i];
        _timeManager.addTime(timeInstance);
    }

    return _timeManager.timePartition;

}

var Answer = {
    Times: renderTimes(AllTimes),
    Count: renderTimes(AllTimes).length
};

//Displays the Answer...
console.log({Answer})