简体   繁体   English

转换JavaScript 2D数组

[英]Transform JavaScript 2D Array

I am creating a dashboard to show data from various stats. 我正在创建一个仪表板来显示各种统计数据。 I am using Django and Google Charts for creating graphs. 我正在使用Django和Google Charts来创建图表。 It has been good so far but I am stuck at one particular case. 到目前为止一直很好,但我陷入了一个特殊情况。

the model class is- 模型类是 -

class Registration(models.Model):
    event_type = models.CharField(max_length=80)
    date_time = models.DateField()
    count = models.IntegerField()

my query is- 我的疑问是 -

Registration.objects.filter(event_type__in=['VHRAssmntCompleted', 
    'VNAAssmntCompleted', 
    'NonsmokersDeclrtn',
    'MWBAssmntCompleted',
    'VHCAssmntCompleted',
    'SV Document Uploads',
    'PapSmear',
    'Mammogram',], 
    date_time__range=(d3,d1)).order_by('date_time')

I get the data in following format: 我得到以下格式的数据:

[["VHR", "2019-02-1", 23],
["VNA", "2019-02-1", 34],
["PAP", "2019-02-1", 50],
["VHR", "2019-02-2", 92],
["VNA", "2019-02-2", 13],
["PAP", "2019-02-2", 65],
["VHR", "2019-02-3", 192],
["VNA", "2019-02-3", 43],
["PAP", "2019-02-3", 11]]

To create a Combo Chart in need the data in following format(something like python dataframe): 要创建需要的组合图表 ,请使用以下格式的数据(类似于python dataframe):

[["date", "VHR", "VNA", "PAP" ],
["2019-02-1", 23,34,50],
["2019-02-2", 92,13,65],
["2019-02-3", 192,43,11]]

I am unable to find a way to do this, either format it using Django ORM query itself or transform using JS. 我无法找到这样做的方法,要么使用Django ORM查询本身格式化,要么使用JS进行转换。

I need help with what approach should I go. 我需要帮助我应该采取什么方法。

you can group them by date, then using Object.entries loop through the grouped result and transform it to [date, "VHR", "VNA", "PAP"] 您可以按日期对它们进行分组,然后使用Object.entries循环分组结果并将其转换为[date, "VHR", "VNA", "PAP"]

 const data = [ ["VHR", "2019-02-1", 23], ["VNA", "2019-02-1", 34], ["PAP", "2019-02-1", 50], ["VHR", "2019-02-2", 92], ["VNA", "2019-02-2", 13], ["PAP", "2019-02-2", 65], ["VHR", "2019-02-3", 192], ["VNA", "2019-02-3", 43], ["PAP", "2019-02-3", 11] ]; const grouped = data.reduce((all, [key, date, value]) => { all[date] = { ...all[date], [key]: value }; return all; }, {}); const result = ["date", "VHR", "VNA", "PAP"].concat( Object.entries(grouped).map(([date, obj]) => [date, ...Object.values(obj)]) ); console.log(result); 

You could take an object for the column position of the values and collect the data by date and column. 您可以为值的列位置获取一个对象,并按日期和列收集数据。

 var data = [["VHR", "2019-02-1", 23], ["VNA", "2019-02-1", 34], ["PAP", "2019-02-1", 50], ["VHR", "2019-02-2", 92], ["VNA", "2019-02-2", 13], ["PAP", "2019-02-2", 65], ["VHR", "2019-02-3", 192], ["VNA", "2019-02-3", 43], ["PAP", "2019-02-3", 11]], cols = { VHR: 1, VNA: 2, PAP: 3 }, result = data.reduce((r, [key, date, value]) => { var row = r.find(([d]) => d === date); if (!row) { r.push(row = [date, 0, 0, 0]); } row[cols[key]] = value; return r; }, [["date", "VHR", "VNA", "PAP"]]); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM