Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从节点中解析的csv文件构建对象数组_Javascript_Arrays_Node.js_Csv_Object - Fatal编程技术网

Javascript 从节点中解析的csv文件构建对象数组

Javascript 从节点中解析的csv文件构建对象数组,javascript,arrays,node.js,csv,object,Javascript,Arrays,Node.js,Csv,Object,我有多个csv文件的形式 模型1A 模型1B 型号2A 模型2B 其中每个csv都是一个数组,即model1A=[1,1,1] 我想解析这些CSV并创建一个包含所有这些模型的数组,其中数组中的每个元素都是对应于一个特定模型的对象,即 finalArray = [ { "model" : "model1", "A" : [1, 1, 1], "B" : [2, 2, 2] }, { "model" : "mode

我有多个csv文件的形式

  • 模型1A
  • 模型1B
  • 型号2A
  • 模型2B
其中每个csv都是一个数组,即
model1A=[1,1,1]

我想解析这些CSV并创建一个包含所有这些模型的数组,其中数组中的每个元素都是对应于一个特定模型的对象,即

finalArray = [ 
  { 
    "model" :   "model1",
    "A"     :   [1, 1, 1],
    "B"     :   [2, 2, 2]
  },
  { 
    "model" :   "model2",
    "A"     :   [3, 3, 3],
    "B"     :   [4, 4, 4]
  }
]
到目前为止,我掌握的代码是

var csv = require('csv');
var fs = require('fs');
var parser = csv.parse();
var util = require('util');
var junk = require('junk');
var _ = require('lodash');
var models = [];


fs.readdir(__dirname+'/data', function(err, files) {
    var model = {};
    _.forEach(files, function(n, key) {

        console.log('Analysing file: ' + n);
        var modelName;
        var modelNum;
        var modelParam;


        modelNum = n.match(/\d+/)[0];
        modelName = 'model' + modelNum;
        modelParam = (n.substring(0, n.indexOf('.'))).replace(modelName,'');

        model.model = modelName;
        model[modelParam] = [];
        models.push(model);

        //if (Object.keys(model).length === 3) {
        //    models.push(model);
        //    model = {};
        //}


        fs.createReadStream(__dirname+'/data/'+n).pipe(csv.parse()).pipe(csv.transform(function(row) {
            model[modelParam].push(row);

        })).on('readable', function(){
            while(this.read()){}
        }).on('end', function() {
            console.log('finished reading file ' + n);
            if (key === (files.length - 1)) {
                fs.writeFile('result.json', JSON.stringify(models), function (err) {
                    if (err) throw err;
                    console.log(models.length + ' model(s) parsed');
                    console.log('done');
                });
            }

        }).on('error', function(error) {
            console.log(error);
        });    
    });
});
我知道我的一个问题是,我很快将模型推到数组中,导致下面表单的最终数组,其中
model1
model2
覆盖

[ { model: 'model2', A: [], B: [] },
  { model: 'model2', A: [], B: [] },
  { model: 'model2', A: [], B: [] },
  { model: 'model2', A: [], B: [] } ]
这就是为什么我尝试了这个代码

if (Object.keys(model).length === 3) {
  models.push(model);
  model = {};
}
但是这当然不能工作,因为
fs.createReadStream
是异步的,我正在用
model={}
清除模型,然后它才能正常运行

我现在处于这样一个阶段,我觉得我在兜圈子,把事情弄得更糟。我想创建一些更通用的东西,但是,现在我很高兴它能在这里介绍的案例中发挥作用,然后我可以考虑改进它

任何帮助都将不胜感激


更新1 按照萨奎布·汗的建议,将
var模型={}
移动到循环内部,这有助于我更接近我的目标,但仍然不正确。以下是目前的结果

[
    {
        "model": "model1",
        "A": [
            [
                "1"
            ],
            [
                "2"
            ],
            [
                "3"
            ],
            [
                "4"
            ]
        ]
    },
    {
        "model": "model1",
        "B": [
            [
                "1"
            ],
            [
                "2"
            ],
            [
                "3"
            ],
            [
                "4"
            ]
        ]
    },
    {
        "model": "model2",
        "A": [
            [
                "1"
            ],
            [
                "2"
            ],
            [
                "3"
            ],
            [
                "4"
            ]
        ]
    },
    {
        "model": "model2",
        "B": [
            [
                "1"
            ],
            [
                "2"
            ],
            [
                "3"
            ],
            [
                "4"
            ]
        ]
    }
]

更新2 同样按照Denys Denysiuk的建议,结果更接近我想要的,但仍然很短

[
    {
        "model": "model1",
        "A": [
            "1",
            "2",
            "3",
            "4"
        ]
    },
    {
        "model": "model1",
        "B": [
            "1",
            "2",
            "3",
            "4"
        ]
    },
    {
        "model": "model2",
        "A": [
            "1",
            "2",
            "3",
            "4"
        ]
    },
    {
        "model": "model2",
        "B": [
            "1",
            "2",
            "3",
            "4"
        ]
    }
]

如果我能以某种方式迭代最终的对象数组,将对象与匹配的
模型
名称合并,那么这就行了。我现在正在浏览这本书,看看我是否能想出一些办法。如果有,我会发回这里。

Node.js是事件驱动的,所以您可以使用事件模块来编写代码:

您的问题似乎是覆盖了数组中以前的条目,因此只有在前一个CSV已完成写入所需的所有内容时,您才应该进入下一步(读取另一个CSV?)


您可以使用事件将此逻辑添加到代码中。

代码中有一个非常小的编码错误

var模型={}应位于forEach循环内

请尝试以下代码:

var csv = require('csv');
var fs = require('fs');
var parser = csv.parse();
var util = require('util');
var junk = require('junk');
var _ = require('lodash');
var models = [];


fs.readdir(__dirname+'/data', function(err, files) {

    _.forEach(files, function(n, key) {

        console.log('Analysing file: ' + n);
        var model = {};
        var modelName;
        var modelNum;
        var modelParam;


        modelNum = n.match(/\d+/)[0];
        modelName = 'model' + modelNum;
        modelParam = (n.substring(0, n.indexOf('.'))).replace(modelName,'');

        model.model = modelName;
        model[modelParam] = [];
        models.push(model);

        //if (Object.keys(model).length === 3) {
        //    models.push(model);
        //    model = {};
        //}


        fs.createReadStream(__dirname+'/data/'+n).pipe(csv.parse()).pipe(csv.transform(function(row) {
            model[modelParam].push(row);

        })).on('readable', function(){
            while(this.read()){}
        }).on('end', function() {
            console.log('finished reading file ' + n);
            if (key === (files.length - 1)) {
                fs.writeFile('result.json', JSON.stringify(models), function (err) {
                    if (err) throw err;
                    console.log(models.length + ' model(s) parsed');
                    console.log('done');
                });
            }

        }).on('error', function(error) {
            console.log(error);
        });    
    });
});
试试这个:

fs.readdir(__dirname+'/data', function(err, files) {

    _.forEach(files, function(n, key) {

        console.log('Analysing file: ' + n);            

        var modelNum = n.match(/\d+/)[0];
        var modelName = 'model' + modelNum;
        var modelParam = (n.substring(0, n.indexOf('.'))).replace(modelName,'');

        var model = {};
        var isNewModel = true;
        for(var i = 0; i < models.length; i++) {
            if(models[i].model == modelName) {
               model = models[i];
               isNewModel = false;
               break;
            }
        }
        if(isNewModel) {
            model.model = modelName;
            models.push(model);
        }

        model[modelParam] = [];

        fs.createReadStream(__dirname+'/data/'+n).pipe(csv.parse()).pipe(csv.transform(function(row) {
            model[modelParam].push(row[0]);

        })).on('readable', function(){
            while(this.read()){}
        }).on('end', function() {
            console.log('finished reading file ' + n);
            if (key === (files.length - 1)) {
                fs.writeFile('result.json', JSON.stringify(models), function (err) {
                    if (err) throw err;
                    console.log(models.length + ' model(s) parsed');
                    console.log('done');
                });
            }

        }).on('error', function(error) {
            console.log(error);
        });    
    });
fs.readdir(uuu dirname+'/data',函数(err,files){
_.forEach(文件,函数(n,键){
log('分析文件:'+n);
var modelNum=n.match(/\d+/)[0];
var modelName='model'+modelNum;
var modelParam=(n.substring(0,n.indexOf('.')).replace(modelName');
var模型={};
var isNewModel=true;
对于(变量i=0;i
似乎,
是数组。您可以尝试
模型[modelParam].push(行[0]);
您可以解释问题是什么,以及您建议如何解决在每次迭代中新模型中的问题。解决方法是尝试查找现有模型,如果找不到,则创建新模型。