在输入之前重构JSON

时间:2018-02-08 11:38:55

标签: javascript jquery json datatables

以下是我的数据库的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"
}

在构建数据表时,我有很多困难/错误引用了作者和日期的嵌套字段。我想要做的是在表中使用它之前以某种方式修改/展平它(使用数据表'dataSrc,如here所述),然后只需调用重构数据我需要多次使用datatables API

所以我现在称之为issued.date-parts.0.0的只是year。结构将改为:

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

1 个答案:

答案 0 :(得分:1)

使用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
}




相关问题