作者:失心人2702939300 | 来源:互联网 | 2023-09-13 06:34
ImloadingdifferentindicatorCSVfilesintoJavaScript,example:我正在将不同的指标CSV文件加载到JavaScript中,例如:
I'm loading different indicator CSV files into Javascript, example:
我正在将不同的指标CSV文件加载到Javascript中,例如:
CSV for population:
id,year,value
AF,1800,3280000
AF,1820,3280000
AF,1870,4207000
AG,1800,37000
AG,1851,37000
AG,1861,37000
For each indicator file I need to:
对于每个指标文件,我需要:
- Gap fill missing years for each entity (id)
每个实体的缺口填充缺失年份(id)
- Find the time span for each entity
查找每个实体的时间跨度
- Find the min and max for each entity
找到每个实体的最小值和最大值
- Find the time span for the indicator
找出指标的时间跨度
- Find the min and max for the indicator
找到指标的最小值和最大值
What is an inexpensive way of performing these operations? Alternatively, is there a good Javascript library for performing these kind of common data operations and storing the data effectively in various object representations?
执行这些操作的廉价方法是什么?或者,是否有一个很好的Javascript库来执行这些常见的数据操作并将数据有效地存储在各种对象表示中?
I'd like the final representation of the above file to look something like:
我希望上面文件的最终表示看起来像:
data = {
population : {
entities :
AF : {
data : {
1800 : 3280000,
1801 : 3280000,
},
entity_meta : {
start : 1800,
end :
min :
max :
},
[...]
indicator_meta : {
start : 1700,
end :
min :
max :
}
[...]
Thanks!
4 个解决方案
2
Lets Assume that you have the CSV data in a 2d array:
让我们假设你有一个2d数组的CSV数据:
var data = [[AF,1800,3280000],
[AF,1820,3280000],
[AF,1870,4207000],
[AG,1800,37000],
[AG,1851,37000],
[AG,1861,37000]]
For this example I will use jQuerys utility functions as it will make the job a lot easier without any real overhead.
对于这个例子,我将使用jQuerys实用程序函数,因为它将使工作更容易,没有任何实际开销。
// we will loop thru all the rows
// if the id does not belong to the entities then we will add the property.
// if the property does exist then we update the values
var entities = {}
$.each(data, function (i, n) {
// set property
if (!entities[n[0]]) {
entities[n[0]] = {
data : {
n[1]: n[2]
},
entity_meta: {
start: n[1],
end: n[1]
min: n[1]
max: n[1]
}
}
// update property
} else {
// add new data property
entities[n[0]]['data'][n[1]] = n[2];
// if the property should change then update it
if ( entities[n[0]]['entity_meta']['min'] > n[1] ) {
entities[n[0]]['entity_meta']['min'] = n[1];
}
}
});
That obviously isn't all the code but it should explain clearly the approach that should be taken.
这显然不是所有的代码,但它应该清楚地解释应该采取的方法。
Also not that your intended final object structure is very much over complicated you should really use arrays where appropriate, especially for entities
and data
.
也不是说你想要的最终对象结构非常复杂,你应该在适当的地方使用数组,特别是对于实体和数据。