Phi*_*ien 14 javascript csv arrays object node.js
我有多个表单的csv文件
其中每个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]
}
]
Run Code Online (Sandbox Code Playgroud)
我到目前为止的代码是
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);
});
});
});
Run Code Online (Sandbox Code Playgroud)
我知道我的一个问题是我很快将模型推送到数组,导致下面的表格的最后一个数组,其中model1被覆盖model2
[ { model: 'model2', A: [], B: [] },
{ model: 'model2', A: [], B: [] },
{ model: 'model2', A: [], B: [] },
{ model: 'model2', A: [], B: [] } ]
Run Code Online (Sandbox Code Playgroud)
这就是我尝试这段代码的原因
if (Object.keys(model).length === 3) {
models.push(model);
model = {};
}
Run Code Online (Sandbox Code Playgroud)
但当然这不起作用,因为它fs.createReadStream是异步的,我正在清理模型,model = {}然后才能正常运行.
我现在正处于舞台上,我觉得自己正在四处乱逛,只是让事情变得更糟.我想创建一些更通用的东西,但是,现在我很高兴让它适用于这里提供的案例,然后我可以看看它是否有所改进.
任何帮助将非常感激!
继saquib khan关于移动var model = {}内部循环的建议帮助我更接近目标,但它仍然不对.以下是当前结果
[
{
"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"
]
]
}
]
Run Code Online (Sandbox Code Playgroud)
根据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"
]
}
]
Run Code Online (Sandbox Code Playgroud)
这可以工作,如果我可以以某种方式迭代最终的对象数组,合并具有匹配model名称的对象.我目前正在查看lodash文档,看看我是否可以解决问题.如果我这样做,我会在这里回复.
小智 2
试试这个:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |