热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JavaScript中的CSV数据的数据操作(填充,最小,最大)?-Dataoperations(fill,min,max)onCSVdatainJavaScript?

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 个解决方案

#1


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.

也不是说你想要的最终对象结构非常复杂,你应该在适当的地方使用数组,特别是对于实体和数据。

#2


1  

Use jQuery AJAX to get the CSV file.

使用jQuery AJAX获取CSV文件。

$.get("test_csv.csv", function(result){
    csvParseAndCalc(result);
});

Use a simple Javascript to parse the CSV and perform the calculations

使用简单的Javascript来解析CSV并执行计算

// assumes your sample data is how all data will look
// proper csv parsing (by the spec) is not used is favor is speed
function csvParseAndCalc(result) {
var entities = {};
var indicator_meta = {"start":null, "end":null, "min":null, "max":null};
var rows = result.split('\n'); //your data doesnt need proper (to spec) csv parsing
// run calculations ignore header row
for(var i=1; i yr) emeta.start = yr;
    if(emeta.end == null || emeta.end  val) emeta.min = val;
    if(emeta.max == null || emeta.max  yr)
        indicator_meta.start = yr;
    if(indicator_meta.end==null || indicator_meta.end  val)
        indicator_meta.min = val;
    if(indicator_meta.max==null || indicator_meta.max 

#3


1  

Maybe, YUI would be helpful for some bulk operations. http://yuilibrary.com/yui/docs/dataschema/dataschema-text.html

也许,YUI会对一些批量操作有所帮助。 http://yuilibrary.com/yui/docs/dataschema/dataschema-text.html

#4


1  

There are Javascript sql database libraries. TaffyDB comes to mind.

有Javascript sql数据库库。想到了TaffyDB。


推荐阅读
author-avatar
失心人2702939300
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有