这是我数据库的JSON输出中的对象示例:

{
    "id": "http://...",
    "type": "example-type",
    "title": "Example title",
    "container-title": "Example container title",
    "page": "1-100",
    "issue": "3",
    "URL": "http://www.url",
    "ISSN": "0123-0123",
    "author": [
        {
            "family": "Smith",
            "given": "John"
        }
    ],
    "issued": {
        "date-parts": [
            [
                "2000"
            ]
        ]
    },
    "keyword": "Sample Tag"
}


在构建数据表时,我遇到了极大的困难/错误,这些错误/错误涉及作者和日期的嵌套字段。我想做的是在表中使用它之前以某种方式对其进行修改/展平(如here所述使用Datatables的dataSrc),然后根据需要使用datatables API多次调用重组数据。

因此,我现在所说的issued.date-parts.0.0就是year。结构将改为:

        "authors": "John Smith", "Mark Smith"
        "year": "2000"

最佳答案

使用map函数获取authors

看看这个代码片段



var data = {    "id": "http://...",    "type": "example-type",    "title": "Example title",    "container-title": "Example container title",    "page": "1-100",    "issue": "3",    "URL": "http://www.url",    "ISSN": "0123-0123",    "author": [        {            "family": "Smith",            "given": "John"        },        {            "family": "Smith",            "given": "Mark"        }    ],    "issued": {        "date-parts": [            [                "2000"            ]        ]    },    "keyword": "Sample Tag"};

var result = {
  "authors": data.author.map((d) => `${d.given} ${d.family}`),
  "year": data.issued['date-parts'][0][0]
}

console.log(result);

.as-console-wrapper {
  max-height: 100% !important
}

关于javascript - 输入之前重组JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48684589/

10-11 13:34