![](/img/trans.png)
[英]Fastest way to convert a integer to arbitrarily ordered byte arrays in JavaScript?
[英]What is the fastest way to convert fields of a json into arrays in javascript?
我有一個內容如下的 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
}
我使用以下代碼達到了預期的結果:
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');
'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
]
}
我想學習一種更好更快的方法來實現相同的目標。 有什么建議?
您不需要創建數組並單獨推入它。 只需使用帶有其中值的數組文字:
for (var prop in GO_data) {
GO_data[prop] = [GO_data[prop]];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.