迭代coffeescript中的JSON对象数组

时间:2014-06-29 06:24:39

标签: coffeescript

您好我有以下数据集:

[
    { date:"somedatehere", series1:"series1Value", series2:"series2Value" ..., seriesX:"seriesXValue"}, 
    { date:"anotherDateHere", series1:"anotherseries1Value", series2:"anotherseries2Value"...,seriesX:"anotherseriesXValue"},...
]

我想在coffeescript中循环显示并提取数组,以便我有一个dates数组(由somedatehereanotherDateHere等组成),{ {1}}值,series1值,series2值等

最好所有这些数组都按顺序排列seriesXdates[0] === somedatehere以及series1[0] === series1Valueseries2[0] === series2Value等。

在coffeescript中有没有简单的方法可以做到这一点?

1 个答案:

答案 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 ]
}