[英]converting a complex array into json in javascript
基本上我想将 Excel 数据转换为复杂的 JSON。 到目前为止,我已将 excel 数据转换为如下所示的行数组。 第一个数组将始终是表格的标题。
const row = [["rule", "key_phrase", "doc_type", "doc_priority", "section_title"],
["approval related", "approved", "assessment report", 1, "4. Recommendations, outcome"],
["prime designation", "prime,prime designation", "assessment-report", 1, "1. Background information on the procedure, 1.1. Submission of the dossier"],
["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "5.1 Pharmaco dymnamic properties, Clinical Efficacy"],
["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "CLINICAL/STATISTICAL/PHARMACOVIGILANCE, Clinical Program"]]
现在我想将该数组转换为 JSON。 每一个逗号,
分隔值应该进入相同的密钥。 例如。 key_phrase: ["prime", "prime designation"] 。
所以从上面的excel数据来看,第一个JSON对象应该是这样的:
{
"biz_rules": [
{
"rule": "approval related",
"key_phrase": [
"approved"
],
"doc": [
{
"type": "assessment-report",
"priority": 1,
"sections": [
{
"title_keywords": [
"4. Recommendations",
"outcome"
],
"content_keywords": [
"marketing authorization"
]
}
]
}
]
},
{
"rule": "prime designation",
"key_phrase": [
"prime",
"prime designation"
],
"doc": [
{
"type": "assessment-report",
"priority": 1,
"sections": [
{
"title_keywords": [
"1. Background information on the procedure",
"1.1. Submission of the dossier"
],
"content_keywords": [
"prime"
]
}
]
}
]
},
{
"rule": "single-arm trials",
"key_phrase": [
"single-arm",
"single arm",
"single",
"arm"
],
"doc": [
{
"type": "NDA",
"priority": 2,
"sections": [
{
"title_keywords": [
"5.1 Pharmacodynamic properties",
"Clinical efficacy"
],
"content_keywords": [
"single-arm trials"
]
},
{
"title_keywords": [
"CLINICAL/STATISTICAL/PHARMACOVIGILANCE",
"Clinical Program"
],
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "assessment-report",
"priority": 1,
"sections": [
{
"title_keywords": [
"5. Clinical efficacy ",
"2.5.2. Main studies"
],
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "Clinical trials",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "press-release",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "press-releases",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "media-releasess",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
}
]
}
]}
忽略content_keywords 。 规则按部分唯一划分。 因此,部分内的每个title_keywords将是 excel 中的单独规则。
任何帮助将非常感激。 提前致谢。
你可以从
const valueToCsv = text => (text.indexOf(', ') > 0) ? text.split(', ') : text; const newArr = arr.slice(1).map(row => ({ rule: valueToCsv(row[0]), key_phrase: valueToCsv(row[1]), doc: [{ type: valueToCsv(row[2]), priority: parseInt(row[3]), sections: [ { title_keywords: valueToCsv(row[4]) } ] }] })); console.log(JSON.stringify(newArr));
这段代码。
您可以尝试以下操作:
const data = [["rule", "key_phrase", "doc_type", "doc_priority", "section_title"], ["approval related", "approved", "assessment report", 1, "4. Recommendations, outcome"], ["prime designation", "prime,prime designation", "assessment-report", 1, "1. Background information on the procedure, 1.1. Submission of the dossier"], ["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "5.1 Pharmaco dymnamic properties, Clinical Efficacy"], ["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "CLINICAL/STATISTICAL/PHARMACOVIGILANCE, Clinical Program"]] data.shift() const obj = data.map(e => { return { rule: e[0], key_phrase: e[1], doc: { doc_type: e[2], doc_priority: e[3], }, section_title: e[4], } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.