您好我有以下数据集:
[
{ date:"somedatehere", series1:"series1Value", series2:"series2Value" ..., seriesX:"seriesXValue"},
{ date:"anotherDateHere", series1:"anotherseries1Value", series2:"anotherseries2Value"...,seriesX:"anotherseriesXValue"},...
]
我想在coffeescript中循环显示并提取数组,以便我有一个dates
数组(由somedatehere
,anotherDateHere
等组成),{ {1}}值,series1
值,series2
值等
最好所有这些数组都按顺序排列seriesX
和dates[0] === somedatehere
以及series1[0] === series1Value
和series2[0] === series2Value
等。
在coffeescript中有没有简单的方法可以做到这一点?
答案 0 :(得分:1)
dates = (obj.date for obj in my_array)
series1 = (obj.series for obj in my_array)
如果您有很多系列并且不想手动枚举它们:
types = (k for k, v of my_array[0])
result = {}
result[type] = (obj[type] for obj in my_array) for type in types
会给你
my_array = [{date: 1, x: 2}, {date: 123, x: 2134}]
result = {
date: [ 1, 123 ],
x: [ 2, 2134 ]
}