Javascript Nodejs-将制表符分隔的文件写入json对象

Javascript Nodejs-将制表符分隔的文件写入json对象,javascript,json,node.js,fs,Javascript,Json,Node.js,Fs,是否有一个npm模块将制表符分隔的文件转换为JSON对象,以便我可以通过某些属性查找数据 示例:文件如下所示 name sex age A M 20 B F 30 C M 40 D F 50 JSON 是的,csvtojson和分隔符可以是任何东西,而不仅仅是逗号。 例如: 是的,csvtojson和分隔符可以是任何东西,而不仅仅是逗号。 例如: 有时我更喜欢不使用节点模块,这样我就可以运行这些脚本而无需任何设置 IE.nodejs./convert-t

是否有一个npm模块将制表符分隔的文件转换为JSON对象,以便我可以通过某些属性查找数据

示例:文件如下所示

name sex age
A    M   20
B    F   30
C    M   40
D    F   50
JSON


是的,csvtojson和分隔符可以是任何东西,而不仅仅是逗号。 例如:


是的,csvtojson和分隔符可以是任何东西,而不仅仅是逗号。 例如:

有时我更喜欢不使用节点模块,这样我就可以运行这些脚本而无需任何设置

IE.nodejs./convert-to-csv.js

下面是一个nodejs脚本,以防您选择不使用节点模块

var fs = require("fs");
fs.readFile("./birthrate_poverty.txt","utf8", function(err, data){
    var rows = data.split("\n");
    var json = [];
    var keys = [];

    rows.forEach((value, index)=>{
        if(index < 1){// get the keys from the first row in the tab space file
            keys = value.split("\t");
        }else {// put the values from the following rows into object literals
            values = value.split("\t");
            json[index-1] = values.map((value, index) => {
                return {
                    [keys[index]]: value
                }
            }).reduce((currentValue, previousValue) => {
                return {
                    ...currentValue,
                    ...previousValue
                }
            });
        }
    })


    // convert array of objects into json str, and then write it back out to a file
    let jsonStr = JSON.stringify(json);
    fs.writeFileSync("./birthrate_poverty.json", jsonStr, {encoding: "utf8"})
});
var fs=require(“fs”);
fs.readFile(“./birthrate\u poverty.txt”,“utf8”,函数(err,data){
var rows=data.split(“\n”);
var json=[];
var键=[];
rows.forEach((值,索引)=>{
if(index<1){//从选项卡空间文件的第一行获取键
键=值。拆分(“\t”);
}else{//将下列行中的值放入对象文本中
value=value.split(“\t”);
json[index-1]=values.map((值,索引)=>{
返回{
[键[索引]]:值
}
}).reduce((当前值,先前值)=>{
返回{
…当前值,
…以前的价值
}
});
}
})
//将对象数组转换为json str,然后将其写回文件
让jsonStr=JSON.stringify(JSON);
fs.writeFileSync(“./birthrate_poverty.json”,jsonStr,{encoding:“utf8”})
});
有时我不喜欢使用节点模块,这样我就可以在不进行任何设置的情况下运行这些脚本

IE.nodejs./convert-to-csv.js

下面是一个nodejs脚本,以防您选择不使用节点模块

var fs = require("fs");
fs.readFile("./birthrate_poverty.txt","utf8", function(err, data){
    var rows = data.split("\n");
    var json = [];
    var keys = [];

    rows.forEach((value, index)=>{
        if(index < 1){// get the keys from the first row in the tab space file
            keys = value.split("\t");
        }else {// put the values from the following rows into object literals
            values = value.split("\t");
            json[index-1] = values.map((value, index) => {
                return {
                    [keys[index]]: value
                }
            }).reduce((currentValue, previousValue) => {
                return {
                    ...currentValue,
                    ...previousValue
                }
            });
        }
    })


    // convert array of objects into json str, and then write it back out to a file
    let jsonStr = JSON.stringify(json);
    fs.writeFileSync("./birthrate_poverty.json", jsonStr, {encoding: "utf8"})
});
var fs=require(“fs”);
fs.readFile(“./birthrate\u poverty.txt”,“utf8”,函数(err,data){
var rows=data.split(“\n”);
var json=[];
var键=[];
rows.forEach((值,索引)=>{
if(index<1){//从选项卡空间文件的第一行获取键
键=值。拆分(“\t”);
}else{//将下列行中的值放入对象文本中
value=value.split(“\t”);
json[index-1]=values.map((值,索引)=>{
返回{
[键[索引]]:值
}
}).reduce((当前值,先前值)=>{
返回{
…当前值,
…以前的价值
}
});
}
})
//将对象数组转换为json str,然后将其写回文件
让jsonStr=JSON.stringify(JSON);
fs.writeFileSync(“./birthrate_poverty.json”,jsonStr,{encoding:“utf8”})
});

请给我们看一个文件示例。以及预期的输出。请给我们一个文件示例。以及预期的产出。
var fs = require("fs");
fs.readFile("./birthrate_poverty.txt","utf8", function(err, data){
    var rows = data.split("\n");
    var json = [];
    var keys = [];

    rows.forEach((value, index)=>{
        if(index < 1){// get the keys from the first row in the tab space file
            keys = value.split("\t");
        }else {// put the values from the following rows into object literals
            values = value.split("\t");
            json[index-1] = values.map((value, index) => {
                return {
                    [keys[index]]: value
                }
            }).reduce((currentValue, previousValue) => {
                return {
                    ...currentValue,
                    ...previousValue
                }
            });
        }
    })


    // convert array of objects into json str, and then write it back out to a file
    let jsonStr = JSON.stringify(json);
    fs.writeFileSync("./birthrate_poverty.json", jsonStr, {encoding: "utf8"})
});