简体   繁体   English

在javascript中将复杂数组转换为json

[英]converting a complex array into json in javascript

Basically I want to convert an Excel Data into a complex JSON.基本上我想将 Excel 数据转换为复杂的 JSON。 So far, I've converted the excel data into array of rows like below.到目前为止,我已将 excel 数据转换为如下所示的行数组。 First array will always be the headings of the table.第一个数组将始终是表格的标题。

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"]]

在此处输入图片说明

And now I want to convert that array into a JSON.现在我想将该数组转换为 JSON。 Every comma , separated values should go into the same key.每一个逗号,分隔值应该进入相同的密钥。 For eg.例如。 key_phrase: ["prime", "prime designation"] . key_phrase: ["prime", "prime designation"]

So from the above excel data, the first JSON object should be something like this:所以从上面的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"
              ]
            }
          ]
        }
      ]
    }
]}

Ignoring content_keywords .忽略content_keywords The rules are uniquely divided as per sections.规则按部分唯一划分。 So every title_keywords inside the sections would be separate rule in excel.因此,部分内的每个title_keywords将是 excel 中的单独规则。

Any help would be much appreciated.任何帮助将非常感激。 Thanks in Advance.提前致谢。

You can start with你可以从

 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));

this code.这段代码。

You can try with following:您可以尝试以下操作:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM