如何在javascript中更简洁地填写时间序列数据中的缺失值? [英] How can I more succinctly fill in missing values in time series data in javascript?

查看:97
本文介绍了如何在javascript中更简洁地填写时间序列数据中的缺失值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在此处发布代码: https://gist.github.com/3102527

I have posted the code here: https://gist.github.com/3102527

此代码采用一系列已排序的对象,其中日期键为sent_hour,格式为00 - 23。有些sent_hours缺失。我的代码填写了。

This code takes an array of sorted objects, where the date key is 'sent_hour' of the format "00"-"23. Some sent_hours are missing. My code fills them in.

这是多行。如何在javascript中用几行简单的方法完成?

It is many lines. How can this be done in javascript in a few simple lines?

推荐答案

试试这个

function fillBlanks(ourHours, rawData) {
    ourHours = []; //init array
    for (var i=0; i<=23; i++) //fill array
        ourHours.push({"sent_hour": makeHourRange(i), "total": 0});

    for (h in rawData) //add data to array
        ourHours[rawData[h].sent_hour*1].total += rawData[h].total;

    return ourHours;
}

function makeHourRange(num) {
    return num < 10 ? "0" + num.toString() : num.toString();
}

这篇关于如何在javascript中更简洁地填写时间序列数据中的缺失值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆