简体   繁体   English

在javascript中将json的字段转换为数组的最快方法是什么?

[英]What is the fastest way to convert fields of a json into arrays in javascript?

I have a JSON file whose content is as follows, I want to replace the content of each field with arrays.我有一个内容如下的 JSON 文件,我想用数组替换每个字段的内容。

{
    "ID": "GO:0051345",
    "Description": "positive regulation of hydrolase activity",
    "GeneRatio": "3/5",
    "BgRatio": "464/15405",
    "pvalue": 0.000259498331261799,
    "p_adjust": 0.0446337129770295,
    "qvalue": 0.0169356805665595,
    "geneID": "Ptpa/Wdr35/Dnajb11",
    "Count": 3
}

I have achieved desired result using the following code:我使用以下代码达到了预期的结果:

GO_data = fs.readFileSync('my_data.json');
GO_data = JSON.parse(GO_data);
var data_header = Object.keys(GO_data);
// enumerate its property names
for (var prop in GO_data) { 
    var arr = [];
    arr.push(GO_data[prop]);
    GO_data[prop] = arr;
}
//write the converted json
fs.writeFileSync('my_data_arr.json', JSON.stringify(GO_data), 'utf8');

content of 'my_data_arr.json' looks like below (as desired), 'my_data_arr.json' 的内容如下所示(根据需要),

{
    "ID": [
        "GO:0051345"
    ],
    "Description": [
        "positive regulation of hydrolase activity"
    ],
    "GeneRatio": [
        "3/5"
    ],
    "BgRatio": [
        "464/15405"
    ],
    "pvalue": [
        0.000259498331261799
    ],
    "p_adjust": [
        0.0446337129770295
    ],
    "qvalue": [
        0.0169356805665595
    ],
    "geneID": [
        "Ptpa/Wdr35/Dnajb11"
    ],
    "Count": [
        3
    ]
}

I would like to learn a better and faster way to achieve the same.我想学习一种更好更快的方法来实现相同的目标。 Any suggestions?有什么建议?

You don't need to create the array and push into it separately.您不需要创建数组并单独推入它。 Just use an array literal with the value in it:只需使用带有其中值的数组文字:

for (var prop in GO_data) {
    GO_data[prop] = [GO_data[prop]];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中将整数转换为任意排序的字节数组的最快方法? - Fastest way to convert a integer to arbitrarily ordered byte arrays in JavaScript? 在 JavaScript 中将 N 个未排序数组合并为 1 个排序数组的最快方法是什么? - What is the fastest way to merge N unsorted arrays into 1 sorted array in JavaScript? 在 JavaScript 中将字符串转换为数字的最快方法是什么? - What's the fastest way to convert String to Number in JavaScript? 将 JSON 转换为 CSV 的最简单方法(包括数组到字段) - Easiest Way to Convert JSON to CSV (Including arrays to fields) 将javascript“几乎数组”转换为“数组”的最干净方法是什么? - What is the cleanest way to convert javascript “almost arrays” into “arrays”? 什么是JavaScript最快的JSON解析器? - What is the fastest JSON parser for JavaScript? 将关联 Javascript 数组拆分为两个不同的 arrays 的最快方法是什么? - What's the fastest way to split an associative Javascript array into two different arrays? 从所有子数组或JavaScript中的数组中删除特定元素的最快方法是什么 - What is the fastest way to remove specifc elements from all sub arrays or an array in javascript 使用 Javascript arrays 计算集差的最快或最优雅的方法是什么? - What is the fastest or most elegant way to compute a set difference using Javascript arrays? 在javascript中将任何字符串转换为数组的最快方法 - Fastest way to convert any string to array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM