gpt4 book ai didi

javascript - 输入前重构 JSON

转载 作者:行者123 更新时间:2023-12-03 02:32:19 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

使用map函数获取作者

看看这段代码

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com